StringUtils.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "il2cpp-config.h"
  2. #include "../char-conversions.h"
  3. #include "il2cpp-object-internals.h"
  4. #include "utils/Functional.h"
  5. #include "utils/Memory.h"
  6. #include "utils/StringUtils.h"
  7. #include "External/utfcpp/source/utf8.h"
  8. #include <stdarg.h>
  9. namespace il2cpp
  10. {
  11. namespace utils
  12. {
  13. std::string StringUtils::Printf(const char* format, ...)
  14. {
  15. va_list argsToCheckSize;
  16. int n;
  17. std::string ret;
  18. va_start(argsToCheckSize, format);
  19. #if IL2CPP_COMPILER_MSVC
  20. // MS vsnprintf always returns -1 if string doesn't fit, rather than
  21. // the needed size. Used their 'special' function instead to get required size
  22. n = _vscprintf_p(format, argsToCheckSize);
  23. #else
  24. // use a temporary buffer as some docs indicate we cannot pass NULL to vsnprintf
  25. char buf[1];
  26. n = vsnprintf(buf, 0, format, argsToCheckSize);
  27. #endif
  28. va_end(argsToCheckSize);
  29. if (n == -1)
  30. return {};
  31. ret.resize(n + 1, 0);
  32. va_list argsToFormat;
  33. va_start(argsToFormat, format);
  34. n = vsnprintf(&ret[0], ret.size(), format, argsToFormat);
  35. va_end(argsToFormat);
  36. IL2CPP_ASSERT(n < (int)ret.size());
  37. if (n == -1)
  38. return {};
  39. // The v*printf methods might put a trailing NUL character, which should not not be in a
  40. // std::string, so strip it out.
  41. if (!ret.empty() && ret[ret.size() - 1] == '\0')
  42. ret = ret.substr(0, ret.size() - 1);
  43. return ret;
  44. }
  45. std::string StringUtils::NPrintf(const char* format, size_t max_n, ...)
  46. {
  47. va_list argsToCheckSize;
  48. size_t n;
  49. std::string ret;
  50. va_start(argsToCheckSize, max_n);
  51. #if IL2CPP_COMPILER_MSVC
  52. // MS vsnprintf always returns -1 if string doesn't fit, rather than
  53. // the needed size. Used their 'special' function instead to get required size
  54. n = _vscprintf_p(format, argsToCheckSize);
  55. #else
  56. // use a temporary buffer as some docs indicate we cannot pass NULL to vsnprintf
  57. char buf[1];
  58. n = vsnprintf(buf, 0, format, argsToCheckSize);
  59. #endif
  60. if (n == -1)
  61. {
  62. va_end(argsToCheckSize);
  63. return {};
  64. }
  65. n = (max_n < ++n) ? max_n : n;
  66. ret.resize(n, 0);
  67. va_end(argsToCheckSize);
  68. va_list argsToFormat;
  69. va_start(argsToFormat, max_n);
  70. n = vsnprintf(&ret[0], n, format, argsToFormat);
  71. va_end(argsToFormat);
  72. IL2CPP_ASSERT(n < ret.size());
  73. if (n == -1)
  74. return {};
  75. // The v*printf methods might put a trailing NUL character, which should not not be in a
  76. // std::string, so strip it out.
  77. if (!ret.empty() && ret[ret.size() - 1] == '\0')
  78. ret = ret.substr(0, ret.size() - 1);
  79. return ret;
  80. }
  81. std::string StringUtils::Utf16ToUtf8(const Il2CppChar* utf16String)
  82. {
  83. return Utf16ToUtf8(utf16String, -1);
  84. }
  85. std::string StringUtils::Utf16ToUtf8(const Il2CppChar* utf16String, int maximumSize)
  86. {
  87. const Il2CppChar* ptr = utf16String;
  88. size_t length = 0;
  89. while (*ptr)
  90. {
  91. ptr++;
  92. length++;
  93. if (maximumSize != -1 && length == maximumSize)
  94. break;
  95. }
  96. std::string utf8String;
  97. utf8String.reserve(length);
  98. utf8::unchecked::utf16to8(utf16String, ptr, std::back_inserter(utf8String));
  99. return utf8String;
  100. }
  101. std::string StringUtils::Utf16ToUtf8(const UTF16String& utf16String)
  102. {
  103. return Utf16ToUtf8(utf16String.c_str(), static_cast<int>(utf16String.length()));
  104. }
  105. UTF16String StringUtils::Utf8ToUtf16(const char* utf8String)
  106. {
  107. return Utf8ToUtf16(utf8String, strlen(utf8String));
  108. }
  109. UTF16String StringUtils::Utf8ToUtf16(const char* utf8String, size_t length)
  110. {
  111. UTF16String utf16String;
  112. if (utf8::is_valid(utf8String, utf8String + length))
  113. {
  114. utf16String.reserve(length);
  115. utf8::unchecked::utf8to16(utf8String, utf8String + length, std::back_inserter(utf16String));
  116. }
  117. return utf16String;
  118. }
  119. UTF16String StringUtils::Utf8ToUtf16(const std::string& utf8String)
  120. {
  121. return Utf8ToUtf16(utf8String.c_str(), utf8String.length());
  122. }
  123. char* StringUtils::StringDuplicate(const char *strSource)
  124. {
  125. char* result = NULL;
  126. if (!strSource)
  127. return NULL;
  128. size_t length = strlen(strSource) + 1;
  129. if ((result = (char*)IL2CPP_MALLOC(length)))
  130. #if IL2CPP_COMPILER_MSVC
  131. strcpy_s(result, length, strSource);
  132. #elif IL2CPP_TARGET_LINUX
  133. strncpy(result, strSource, length);
  134. #else
  135. strlcpy(result, strSource, length);
  136. #endif
  137. return result;
  138. }
  139. Il2CppChar* StringUtils::StringDuplicate(const Il2CppChar* strSource, size_t length)
  140. {
  141. size_t byteLengthWithNullTerminator = sizeof(Il2CppChar) * (length + 1);
  142. Il2CppChar* utf16name = (Il2CppChar*)IL2CPP_MALLOC(byteLengthWithNullTerminator);
  143. memcpy(utf16name, strSource, byteLengthWithNullTerminator);
  144. return utf16name;
  145. }
  146. void StringUtils::StringDelete(const char* str)
  147. {
  148. IL2CPP_FREE((void*)str);
  149. }
  150. bool StringUtils::EndsWith(const std::string& string, const std::string& suffix)
  151. {
  152. const size_t stringLength = string.length();
  153. const size_t suffixLength = suffix.length();
  154. if (suffixLength > stringLength)
  155. return false;
  156. return string.rfind(suffix.c_str(), stringLength - suffixLength, suffixLength) != std::string::npos;
  157. }
  158. Il2CppChar* StringUtils::GetChars(Il2CppString* str)
  159. {
  160. return str->chars;
  161. }
  162. int32_t StringUtils::GetLength(Il2CppString* str)
  163. {
  164. return str->length;
  165. }
  166. } /* utils */
  167. } /* il2cpp */