LLVM 23.0.0git
StringExtras.cpp
Go to the documentation of this file.
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
16#include <cctype>
17
18using namespace llvm;
19
20/// getToken - This function extracts one token from source, ignoring any
21/// leading characters that appear in the Delimiters string, and ending the
22/// token at any of the characters that appear in the Delimiters string. If
23/// there are no tokens in the source string, an empty string is returned.
24/// The function returns a pair containing the extracted token and the
25/// remaining tail string.
26std::pair<StringRef, StringRef> llvm::getToken(StringRef Source,
27 StringRef Delimiters) {
28 // Figure out where the token starts.
29 StringRef::size_type Start = Source.find_first_not_of(Delimiters);
30
31 // Find the next occurrence of the delimiter.
32 StringRef::size_type End = Source.find_first_of(Delimiters, Start);
33
34 return {Source.slice(Start, End), Source.substr(End)};
35}
36
37/// SplitString - Split up the specified string according to the specified
38/// delimiters, appending the result fragments to the output list.
40 SmallVectorImpl<StringRef> &OutFragments,
41 StringRef Delimiters) {
42 std::pair<StringRef, StringRef> S = getToken(Source, Delimiters);
43 while (!S.first.empty()) {
44 OutFragments.push_back(S.first);
45 S = getToken(S.second, Delimiters);
46 }
47}
48
50 for (unsigned char C : Name) {
51 if (C == '\\')
52 Out << '\\' << C;
53 else if (isPrint(C) && C != '"')
54 Out << C;
55 else
56 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
57 }
58}
59
61 for (char C : String) {
62 if (C == '&')
63 Out << "&amp;";
64 else if (C == '<')
65 Out << "&lt;";
66 else if (C == '>')
67 Out << "&gt;";
68 else if (C == '\"')
69 Out << "&quot;";
70 else if (C == '\'')
71 Out << "&apos;";
72 else
73 Out << C;
74 }
75}
76
78 for (const char C : String)
79 Out << toLower(C);
80}
81
83 for (unsigned char C : String) {
84 if (isAlnum(C) || StringRef("-_.~").contains(C))
85 Out << C;
86 else
87 Out << '%' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
88 }
89}
90
92 if (input.empty())
93 return "";
94
95 std::string snakeCase;
96 snakeCase.reserve(input.size());
97 auto check = [&input](size_t j, function_ref<bool(int)> predicate) {
98 return j < input.size() && predicate(input[j]);
99 };
100 for (size_t i = 0; i < input.size(); ++i) {
101 snakeCase.push_back(tolower(input[i]));
102 // Handles "runs" of capitals, such as in OPName -> op_name.
103 if (check(i, isupper) && check(i + 1, isupper) && check(i + 2, islower))
104 snakeCase.push_back('_');
105 if ((check(i, islower) || check(i, isdigit)) && check(i + 1, isupper))
106 snakeCase.push_back('_');
107 }
108 return snakeCase;
109}
110
112 bool capitalizeFirst) {
113 if (input.empty())
114 return "";
115
116 std::string output;
117 output.reserve(input.size());
118
119 // Push the first character, capatilizing if necessary.
120 if (capitalizeFirst && std::islower(input.front()))
121 output.push_back(llvm::toUpper(input.front()));
122 else
123 output.push_back(input.front());
124
125 // Walk the input converting any `*_[a-z]` snake case into `*[A-Z]` camelCase.
126 for (size_t pos = 1, e = input.size(); pos < e; ++pos) {
127 if (input[pos] == '_' && pos != (e - 1) && std::islower(input[pos + 1]))
128 output.push_back(llvm::toUpper(input[++pos]));
129 else
130 output.push_back(input[pos]);
131 }
132 return output;
133}
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
size_t size_type
Definition StringRef.h:62
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
char front() const
Get the first character in the string.
Definition StringRef.h:147
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
char toLower(char x)
Returns the corresponding lowercase character if x is uppercase.
LLVM_ABI void printHTMLEscaped(StringRef String, raw_ostream &Out)
Print each character of the specified string, escaping HTML special characters.
LLVM_ABI void printLowerCase(StringRef String, raw_ostream &Out)
printLowerCase - Print each character as lowercase if it is uppercase.
LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
LLVM_ABI void printEscapedString(StringRef Name, raw_ostream &Out)
Print each character of the specified string, escaping it if it is not printable or if it is an escap...
LLVM_ABI void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")
SplitString - Split up the specified string according to the specified delimiters,...
LLVM_ABI std::string convertToSnakeFromCamelCase(StringRef input)
Converts a string from camel-case to snake-case by replacing all uppercase letters with '_' followed ...
LLVM_ABI void printPercentEncoded(StringRef String, raw_ostream &Out)
Print each character of String percent-encoded for use as a URL query-component value (RFC 3986): unr...
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
char toUpper(char x)
Returns the corresponding uppercase character if x is lowercase.
LLVM_ABI std::string convertToCamelFromSnakeCase(StringRef input, bool capitalizeFirst=false)
Converts a string from snake-case to camel-case by replacing all occurrences of '_' followed by a low...
bool isPrint(char C)
Checks whether character C is printable.