string_util_win.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/containers/span.h"
  14. #include "base/logging.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. // The following section contains overloads of the cross-platform APIs for
  89. // std::wstring and base::WStringPiece. These are only enabled if std::wstring
  90. // and base::string16 are distinct types, as otherwise this would result in an
  91. // ODR violation.
  92. // TODO(crbug.com/911896): Remove those guards once base::string16 is
  93. // std::u16string.
  94. #if defined(BASE_STRING16_IS_STD_U16STRING)
  95. BASE_EXPORT bool IsStringASCII(WStringPiece str);
  96. BASE_EXPORT std::wstring ToLowerASCII(WStringPiece str);
  97. BASE_EXPORT std::wstring ToUpperASCII(WStringPiece str);
  98. BASE_EXPORT int CompareCaseInsensitiveASCII(WStringPiece a, WStringPiece b);
  99. BASE_EXPORT bool EqualsCaseInsensitiveASCII(WStringPiece a, WStringPiece b);
  100. BASE_EXPORT bool RemoveChars(WStringPiece input,
  101. WStringPiece remove_chars,
  102. std::wstring* output);
  103. BASE_EXPORT bool ReplaceChars(WStringPiece input,
  104. WStringPiece replace_chars,
  105. WStringPiece replace_with,
  106. std::wstring* output);
  107. BASE_EXPORT bool TrimString(WStringPiece input,
  108. WStringPiece trim_chars,
  109. std::string* output);
  110. BASE_EXPORT WStringPiece TrimString(WStringPiece input,
  111. WStringPiece trim_chars,
  112. TrimPositions positions);
  113. BASE_EXPORT TrimPositions TrimWhitespace(WStringPiece input,
  114. TrimPositions positions,
  115. std::wstring* output);
  116. BASE_EXPORT WStringPiece TrimWhitespace(WStringPiece input,
  117. TrimPositions positions);
  118. BASE_EXPORT std::wstring CollapseWhitespace(
  119. WStringPiece text,
  120. bool trim_sequences_with_line_breaks);
  121. BASE_EXPORT bool ContainsOnlyChars(WStringPiece input, WStringPiece characters);
  122. BASE_EXPORT bool LowerCaseEqualsASCII(WStringPiece str,
  123. StringPiece lowecase_ascii);
  124. BASE_EXPORT bool EqualsASCII(StringPiece16 str, StringPiece ascii);
  125. BASE_EXPORT bool StartsWith(WStringPiece str,
  126. WStringPiece search_for,
  127. CompareCase case_sensitivity);
  128. BASE_EXPORT bool EndsWith(WStringPiece str,
  129. WStringPiece search_for,
  130. CompareCase case_sensitivity);
  131. BASE_EXPORT void ReplaceFirstSubstringAfterOffset(std::wstring* str,
  132. size_t start_offset,
  133. WStringPiece find_this,
  134. WStringPiece replace_with);
  135. BASE_EXPORT void ReplaceSubstringsAfterOffset(std::wstring* str,
  136. size_t start_offset,
  137. WStringPiece find_this,
  138. WStringPiece replace_with);
  139. BASE_EXPORT wchar_t* WriteInto(std::wstring* str, size_t length_with_null);
  140. BASE_EXPORT std::wstring JoinString(span<const std::wstring> parts,
  141. WStringPiece separator);
  142. BASE_EXPORT std::wstring JoinString(span<const WStringPiece> parts,
  143. WStringPiece separator);
  144. BASE_EXPORT std::wstring JoinString(std::initializer_list<WStringPiece> parts,
  145. WStringPiece separator);
  146. BASE_EXPORT std::wstring ReplaceStringPlaceholders(
  147. WStringPiece format_string,
  148. const std::vector<string16>& subst,
  149. std::vector<size_t>* offsets);
  150. #endif
  151. } // namespace base
  152. #endif // BASE_STRINGS_STRING_UTIL_WIN_H_