string_util_win.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_STRINGS_STRING_UTIL_WIN_H_
  5. #define BASE_STRINGS_STRING_UTIL_WIN_H_
  6. #include <stdarg.h>
  7. #include <stddef.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <wchar.h>
  11. #include <string>
  12. #include <vector>
  13. #include "base/check.h"
  14. #include "base/containers/span.h"
  15. #include "base/strings/string16.h"
  16. #include "base/strings/string_piece.h"
  17. #include "base/strings/string_util.h"
  18. namespace base {
  19. // Chromium code style is to not use malloc'd strings; this is only for use
  20. // for interaction with APIs that require it.
  21. inline char* strdup(const char* str) {
  22. return _strdup(str);
  23. }
  24. inline int vsnprintf(char* buffer, size_t size,
  25. const char* format, va_list arguments) {
  26. int length = vsnprintf_s(buffer, size, size - 1, format, arguments);
  27. if (length < 0)
  28. return _vscprintf(format, arguments);
  29. return length;
  30. }
  31. inline int vswprintf(wchar_t* buffer, size_t size,
  32. const wchar_t* format, va_list arguments) {
  33. DCHECK(IsWprintfFormatPortable(format));
  34. int length = _vsnwprintf_s(buffer, size, size - 1, format, arguments);
  35. if (length < 0)
  36. return _vscwprintf(format, arguments);
  37. return length;
  38. }
  39. // Utility functions to access the underlying string buffer as a wide char
  40. // pointer.
  41. //
  42. // Note: These functions violate strict aliasing when char16 and wchar_t are
  43. // unrelated types. We thus pass -fno-strict-aliasing to the compiler on
  44. // non-Windows platforms [1], and rely on it being off in Clang's CL mode [2].
  45. //
  46. // [1] https://crrev.com/b9a0976622/build/config/compiler/BUILD.gn#244
  47. // [2]
  48. // https://github.com/llvm/llvm-project/blob/1e28a66/clang/lib/Driver/ToolChains/Clang.cpp#L3949
  49. inline wchar_t* as_writable_wcstr(char16* str) {
  50. return reinterpret_cast<wchar_t*>(str);
  51. }
  52. inline wchar_t* as_writable_wcstr(string16& str) {
  53. return reinterpret_cast<wchar_t*>(data(str));
  54. }
  55. inline const wchar_t* as_wcstr(const char16* str) {
  56. return reinterpret_cast<const wchar_t*>(str);
  57. }
  58. inline const wchar_t* as_wcstr(StringPiece16 str) {
  59. return reinterpret_cast<const wchar_t*>(str.data());
  60. }
  61. // Utility functions to access the underlying string buffer as a char16 pointer.
  62. inline char16* as_writable_u16cstr(wchar_t* str) {
  63. return reinterpret_cast<char16*>(str);
  64. }
  65. inline char16* as_writable_u16cstr(std::wstring& str) {
  66. return reinterpret_cast<char16*>(data(str));
  67. }
  68. inline const char16* as_u16cstr(const wchar_t* str) {
  69. return reinterpret_cast<const char16*>(str);
  70. }
  71. inline const char16* as_u16cstr(WStringPiece str) {
  72. return reinterpret_cast<const char16*>(str.data());
  73. }
  74. // Utility functions to convert between base::WStringPiece and
  75. // base::StringPiece16.
  76. inline WStringPiece AsWStringPiece(StringPiece16 str) {
  77. return WStringPiece(as_wcstr(str.data()), str.size());
  78. }
  79. inline StringPiece16 AsStringPiece16(WStringPiece str) {
  80. return StringPiece16(as_u16cstr(str.data()), str.size());
  81. }
  82. inline std::wstring AsWString(StringPiece16 str) {
  83. return std::wstring(as_wcstr(str.data()), str.size());
  84. }
  85. inline string16 AsString16(WStringPiece str) {
  86. return string16(as_u16cstr(str.data()), str.size());
  87. }
  88. // Compatibility shim for cross-platform code that passes a StringPieceType to a
  89. // cross platform string utility function. Most of these functions are only
  90. // implemented for base::StringPiece and base::StringPiece16, which is why
  91. // base::WStringPieces need to be converted on API boundaries.
  92. inline StringPiece16 AsCrossPlatformPiece(WStringPiece str) {
  93. return AsStringPiece16(str);
  94. }
  95. inline WStringPiece AsNativeStringPiece(StringPiece16 str) {
  96. return AsWStringPiece(str);
  97. }
  98. // The following section contains overloads of the cross-platform APIs for
  99. // std::wstring and base::WStringPiece. These are only enabled if std::wstring
  100. // and base::string16 are distinct types, as otherwise this would result in an
  101. // ODR violation.
  102. // TODO(crbug.com/911896): Remove those guards once base::string16 is
  103. // std::u16string.
  104. #if defined(BASE_STRING16_IS_STD_U16STRING)
  105. BASE_EXPORT bool IsStringASCII(WStringPiece str);
  106. BASE_EXPORT std::wstring ToLowerASCII(WStringPiece str);
  107. BASE_EXPORT std::wstring ToUpperASCII(WStringPiece str);
  108. BASE_EXPORT int CompareCaseInsensitiveASCII(WStringPiece a, WStringPiece b);
  109. BASE_EXPORT bool EqualsCaseInsensitiveASCII(WStringPiece a, WStringPiece b);
  110. BASE_EXPORT bool RemoveChars(WStringPiece input,
  111. WStringPiece remove_chars,
  112. std::wstring* output);
  113. BASE_EXPORT bool ReplaceChars(WStringPiece input,
  114. WStringPiece replace_chars,
  115. WStringPiece replace_with,
  116. std::wstring* output);
  117. BASE_EXPORT bool TrimString(WStringPiece input,
  118. WStringPiece trim_chars,
  119. std::wstring* output);
  120. BASE_EXPORT WStringPiece TrimString(WStringPiece input,
  121. WStringPiece trim_chars,
  122. TrimPositions positions);
  123. BASE_EXPORT TrimPositions TrimWhitespace(WStringPiece input,
  124. TrimPositions positions,
  125. std::wstring* output);
  126. BASE_EXPORT WStringPiece TrimWhitespace(WStringPiece input,
  127. TrimPositions positions);
  128. BASE_EXPORT std::wstring CollapseWhitespace(
  129. WStringPiece text,
  130. bool trim_sequences_with_line_breaks);
  131. BASE_EXPORT bool ContainsOnlyChars(WStringPiece input, WStringPiece characters);
  132. BASE_EXPORT bool LowerCaseEqualsASCII(WStringPiece str,
  133. StringPiece lowecase_ascii);
  134. BASE_EXPORT bool EqualsASCII(StringPiece16 str, StringPiece ascii);
  135. BASE_EXPORT bool StartsWith(
  136. WStringPiece str,
  137. WStringPiece search_for,
  138. CompareCase case_sensitivity = CompareCase::SENSITIVE);
  139. BASE_EXPORT bool EndsWith(
  140. WStringPiece str,
  141. WStringPiece search_for,
  142. CompareCase case_sensitivity = CompareCase::SENSITIVE);
  143. BASE_EXPORT void ReplaceFirstSubstringAfterOffset(std::wstring* str,
  144. size_t start_offset,
  145. WStringPiece find_this,
  146. WStringPiece replace_with);
  147. BASE_EXPORT void ReplaceSubstringsAfterOffset(std::wstring* str,
  148. size_t start_offset,
  149. WStringPiece find_this,
  150. WStringPiece replace_with);
  151. BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null);
  152. BASE_EXPORT std::wstring JoinString(span<const std::wstring> parts,
  153. WStringPiece separator);
  154. BASE_EXPORT std::wstring JoinString(span<const WStringPiece> parts,
  155. WStringPiece separator);
  156. BASE_EXPORT std::wstring JoinString(std::initializer_list<WStringPiece> parts,
  157. WStringPiece separator);
  158. BASE_EXPORT std::wstring ReplaceStringPlaceholders(
  159. WStringPiece format_string,
  160. const std::vector<string16>& subst,
  161. std::vector<size_t>* offsets);
  162. #endif
  163. } // namespace base
  164. #endif // BASE_STRINGS_STRING_UTIL_WIN_H_