1 //===-- StringExtras.cpp - Implement the StringExtras header --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the StringExtras.h header
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/Support/raw_ostream.h"
20 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
21 /// occurrence of string 's1' in string 's2', ignoring case. Returns
22 /// the offset of s2 in s1 or npos if s2 cannot be found.
23 StringRef::size_type
llvm::StrInStrNoCase(StringRef s1
, StringRef s2
) {
24 size_t N
= s2
.size(), M
= s1
.size();
26 return StringRef::npos
;
27 for (size_t i
= 0, e
= M
- N
+ 1; i
!= e
; ++i
)
28 if (s1
.substr(i
, N
).equals_insensitive(s2
))
30 return StringRef::npos
;
33 /// getToken - This function extracts one token from source, ignoring any
34 /// leading characters that appear in the Delimiters string, and ending the
35 /// token at any of the characters that appear in the Delimiters string. If
36 /// there are no tokens in the source string, an empty string is returned.
37 /// The function returns a pair containing the extracted token and the
38 /// remaining tail string.
39 std::pair
<StringRef
, StringRef
> llvm::getToken(StringRef Source
,
40 StringRef Delimiters
) {
41 // Figure out where the token starts.
42 StringRef::size_type Start
= Source
.find_first_not_of(Delimiters
);
44 // Find the next occurrence of the delimiter.
45 StringRef::size_type End
= Source
.find_first_of(Delimiters
, Start
);
47 return std::make_pair(Source
.slice(Start
, End
), Source
.substr(End
));
50 /// SplitString - Split up the specified string according to the specified
51 /// delimiters, appending the result fragments to the output list.
52 void llvm::SplitString(StringRef Source
,
53 SmallVectorImpl
<StringRef
> &OutFragments
,
54 StringRef Delimiters
) {
55 std::pair
<StringRef
, StringRef
> S
= getToken(Source
, Delimiters
);
56 while (!S
.first
.empty()) {
57 OutFragments
.push_back(S
.first
);
58 S
= getToken(S
.second
, Delimiters
);
62 void llvm::printEscapedString(StringRef Name
, raw_ostream
&Out
) {
63 for (unsigned i
= 0, e
= Name
.size(); i
!= e
; ++i
) {
64 unsigned char C
= Name
[i
];
67 else if (isPrint(C
) && C
!= '"')
70 Out
<< '\\' << hexdigit(C
>> 4) << hexdigit(C
& 0x0F);
74 void llvm::printHTMLEscaped(StringRef String
, raw_ostream
&Out
) {
75 for (char C
: String
) {
91 void llvm::printLowerCase(StringRef String
, raw_ostream
&Out
) {
92 for (const char C
: String
)
96 std::string
llvm::convertToSnakeFromCamelCase(StringRef input
) {
100 std::string snakeCase
;
101 snakeCase
.reserve(input
.size());
102 for (char c
: input
) {
103 if (!std::isupper(c
)) {
104 snakeCase
.push_back(c
);
108 if (!snakeCase
.empty() && snakeCase
.back() != '_')
109 snakeCase
.push_back('_');
110 snakeCase
.push_back(llvm::toLower(c
));
115 std::string
llvm::convertToCamelFromSnakeCase(StringRef input
,
116 bool capitalizeFirst
) {
121 output
.reserve(input
.size());
123 // Push the first character, capatilizing if necessary.
124 if (capitalizeFirst
&& std::islower(input
.front()))
125 output
.push_back(llvm::toUpper(input
.front()));
127 output
.push_back(input
.front());
129 // Walk the input converting any `*_[a-z]` snake case into `*[A-Z]` camelCase.
130 for (size_t pos
= 1, e
= input
.size(); pos
< e
; ++pos
) {
131 if (input
[pos
] == '_' && pos
!= (e
- 1) && std::islower(input
[pos
+ 1]))
132 output
.push_back(llvm::toUpper(input
[++pos
]));
134 output
.push_back(input
[pos
]);