StringUtfUtils.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/std/string/conversions.h>
  10. namespace LyShine
  11. {
  12. //! \brief Returns the number of UTF8 characters in a string.
  13. //!
  14. //! AZStd::string::length() counts individual bytes in the string buffer
  15. //! whereas this function will consider multi-byte chars as one element/
  16. //! character in the string.
  17. inline int GetUtf8StringLength(const AZStd::string& utf8String)
  18. {
  19. AZStd::wstring utf8StringW;
  20. AZStd::to_wstring(utf8StringW, utf8String.c_str());
  21. return static_cast<int>(utf8StringW.size());
  22. }
  23. //! \brief Returns the number of bytes of the size of the given multi-byte char.
  24. inline int GetMultiByteCharSize(const uint32_t multiByteChar)
  25. {
  26. // determine the number of bytes that this Unicode code point uses in utf-8 format
  27. // NOTE: this assumes the uint32_t can be interpreted as a wchar_t, it seems to
  28. // work for cases tested but may not in general.
  29. // In the long run it would be better to eliminate
  30. // this function and use some sequence_lenght function that is not internal.
  31. return static_cast<int>(Utf8::Unchecked::utf8_codepoint_length(multiByteChar));
  32. }
  33. inline int GetByteLengthOfUtf8Chars(const char* utf8String, int numUtf8Chars)
  34. {
  35. AZStd::wstring utf8StringW;
  36. AZStd::to_wstring(utf8StringW, utf8String);
  37. AZStd::wstring::const_iterator pChar = utf8StringW.begin();
  38. int byteStrlen = 0;
  39. for (int i = 0; i < numUtf8Chars; i++)
  40. {
  41. uint32_t ch = *pChar;
  42. if (!ch)
  43. {
  44. break;
  45. }
  46. byteStrlen += GetMultiByteCharSize(ch);
  47. pChar++;
  48. }
  49. return byteStrlen;
  50. }
  51. }