StringUtility.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.11.22
  7. #pragma once
  8. #include <algorithm>
  9. #include <cctype>
  10. #include <iterator>
  11. #include <string>
  12. #include <vector>
  13. namespace WwiseGTE
  14. {
  15. inline std::wstring ConvertNarrowToWide(std::string const& input)
  16. {
  17. std::wstring output;
  18. std::transform(input.begin(), input.end(), std::back_inserter(output),
  19. [](char c) { return static_cast<wchar_t>(c); });
  20. return output;
  21. }
  22. inline std::string ConvertWideToNarrow(std::wstring const& input)
  23. {
  24. std::string output;
  25. std::transform(input.begin(), input.end(), std::back_inserter(output),
  26. [](wchar_t c) { return static_cast<char>(c); });
  27. return output;
  28. }
  29. inline std::string ToLower(std::string const& input)
  30. {
  31. std::string output;
  32. std::transform(input.begin(), input.end(), std::back_inserter(output),
  33. [](int c) { return static_cast<char>(::tolower(c)); });
  34. return output;
  35. }
  36. inline std::string ToUpper(std::string const& input)
  37. {
  38. std::string output;
  39. std::transform(input.begin(), input.end(), std::back_inserter(output),
  40. [](int c) { return static_cast<char>(::toupper(c)); });
  41. return output;
  42. }
  43. // In the default locale for C++ strings, the whitespace characters are
  44. // space (0x20, ' '), form feed (0x0C, '\f'), line feed (0x0A, '\n'),
  45. // carriage return (0x0D, 'r'), horizontal tab (0x09, 't') and
  46. // vertical tab (0x0B, '\v'). See
  47. // https://en.cppreference.com/w/cpp/string/byte/isspace
  48. // for a table of ASCII values and related is* and isw* functions (with
  49. // 'int ch' input) that return 0 or !0.
  50. inline void GetTokens(std::string const& input, std::string const& whiteSpace,
  51. std::vector<std::string>& tokens)
  52. {
  53. std::string tokenString(input);
  54. tokens.clear();
  55. while (tokenString.length() > 0)
  56. {
  57. // Find the beginning of a token.
  58. auto begin = tokenString.find_first_not_of(whiteSpace);
  59. if (begin == std::string::npos)
  60. {
  61. // All tokens have been found.
  62. break;
  63. }
  64. // Strip off the white space.
  65. if (begin > 0)
  66. {
  67. tokenString = tokenString.substr(begin);
  68. }
  69. // Find the end of the token.
  70. auto end = tokenString.find_first_of(whiteSpace);
  71. if (end != std::string::npos)
  72. {
  73. std::string token = tokenString.substr(0, end);
  74. tokens.push_back(token);
  75. tokenString = tokenString.substr(end);
  76. }
  77. else
  78. {
  79. // This is the last token.
  80. tokens.push_back(tokenString);
  81. break;
  82. }
  83. }
  84. }
  85. // For basic text extraction, choose 'whiteSpace' to be ASCII values
  86. // 0x00-0x20,0x7F-0xFF in GetTokens(...).
  87. inline void GetTextTokens(std::string const& input,
  88. std::vector<std::string>& tokens)
  89. {
  90. static std::string const whiteSpace = []
  91. {
  92. std::string temp;
  93. for (int i = 0; i <= 32; ++i)
  94. {
  95. temp += char(i);
  96. }
  97. for (int i = 127; i < 255; ++i)
  98. {
  99. temp += char(i);
  100. }
  101. return temp;
  102. }
  103. ();
  104. GetTokens(input, whiteSpace, tokens);
  105. }
  106. // For advanced text extraction, choose 'whiteSpace' to be ASCII values
  107. // 0x00-0x20,0x7F in GetTokens(...). Any special characters for ASCII
  108. // values 0x80 or larger are retained as text.
  109. inline void GetAdvancedTextTokens(std::string const& input,
  110. std::vector<std::string>& tokens)
  111. {
  112. static std::string const whiteSpace = []
  113. {
  114. std::string temp;
  115. for (int i = 0; i <= 32; ++i)
  116. {
  117. temp += char(i);
  118. }
  119. temp += char(127);
  120. return temp;
  121. }
  122. ();
  123. GetTokens(input, whiteSpace, tokens);
  124. }
  125. }