StringExtras.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //===-- StringExtras.cpp - Implement the StringExtras header --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the StringExtras.h header
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include <cctype>
  16. using namespace llvm;
  17. /// StrInStrNoCase - Portable version of strcasestr. Locates the first
  18. /// occurrence of string 's1' in string 's2', ignoring case. Returns
  19. /// the offset of s2 in s1 or npos if s2 cannot be found.
  20. StringRef::size_type llvm::StrInStrNoCase(StringRef s1, StringRef s2) {
  21. size_t N = s2.size(), M = s1.size();
  22. if (N > M)
  23. return StringRef::npos;
  24. for (size_t i = 0, e = M - N + 1; i != e; ++i)
  25. if (s1.substr(i, N).equals_insensitive(s2))
  26. return i;
  27. return StringRef::npos;
  28. }
  29. /// getToken - This function extracts one token from source, ignoring any
  30. /// leading characters that appear in the Delimiters string, and ending the
  31. /// token at any of the characters that appear in the Delimiters string. If
  32. /// there are no tokens in the source string, an empty string is returned.
  33. /// The function returns a pair containing the extracted token and the
  34. /// remaining tail string.
  35. std::pair<StringRef, StringRef> llvm::getToken(StringRef Source,
  36. StringRef Delimiters) {
  37. // Figure out where the token starts.
  38. StringRef::size_type Start = Source.find_first_not_of(Delimiters);
  39. // Find the next occurrence of the delimiter.
  40. StringRef::size_type End = Source.find_first_of(Delimiters, Start);
  41. return std::make_pair(Source.slice(Start, End), Source.substr(End));
  42. }
  43. /// SplitString - Split up the specified string according to the specified
  44. /// delimiters, appending the result fragments to the output list.
  45. void llvm::SplitString(StringRef Source,
  46. SmallVectorImpl<StringRef> &OutFragments,
  47. StringRef Delimiters) {
  48. std::pair<StringRef, StringRef> S = getToken(Source, Delimiters);
  49. while (!S.first.empty()) {
  50. OutFragments.push_back(S.first);
  51. S = getToken(S.second, Delimiters);
  52. }
  53. }
  54. void llvm::printEscapedString(StringRef Name, raw_ostream &Out) {
  55. for (unsigned char C : Name) {
  56. if (C == '\\')
  57. Out << '\\' << C;
  58. else if (isPrint(C) && C != '"')
  59. Out << C;
  60. else
  61. Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
  62. }
  63. }
  64. void llvm::printHTMLEscaped(StringRef String, raw_ostream &Out) {
  65. for (char C : String) {
  66. if (C == '&')
  67. Out << "&amp;";
  68. else if (C == '<')
  69. Out << "&lt;";
  70. else if (C == '>')
  71. Out << "&gt;";
  72. else if (C == '\"')
  73. Out << "&quot;";
  74. else if (C == '\'')
  75. Out << "&apos;";
  76. else
  77. Out << C;
  78. }
  79. }
  80. void llvm::printLowerCase(StringRef String, raw_ostream &Out) {
  81. for (const char C : String)
  82. Out << toLower(C);
  83. }
  84. std::string llvm::convertToSnakeFromCamelCase(StringRef input) {
  85. if (input.empty())
  86. return "";
  87. std::string snakeCase;
  88. snakeCase.reserve(input.size());
  89. for (char c : input) {
  90. if (!std::isupper(c)) {
  91. snakeCase.push_back(c);
  92. continue;
  93. }
  94. if (!snakeCase.empty() && snakeCase.back() != '_')
  95. snakeCase.push_back('_');
  96. snakeCase.push_back(llvm::toLower(c));
  97. }
  98. return snakeCase;
  99. }
  100. std::string llvm::convertToCamelFromSnakeCase(StringRef input,
  101. bool capitalizeFirst) {
  102. if (input.empty())
  103. return "";
  104. std::string output;
  105. output.reserve(input.size());
  106. // Push the first character, capatilizing if necessary.
  107. if (capitalizeFirst && std::islower(input.front()))
  108. output.push_back(llvm::toUpper(input.front()));
  109. else
  110. output.push_back(input.front());
  111. // Walk the input converting any `*_[a-z]` snake case into `*[A-Z]` camelCase.
  112. for (size_t pos = 1, e = input.size(); pos < e; ++pos) {
  113. if (input[pos] == '_' && pos != (e - 1) && std::islower(input[pos + 1]))
  114. output.push_back(llvm::toUpper(input[++pos]));
  115. else
  116. output.push_back(input[pos]);
  117. }
  118. return output;
  119. }