/src/hermes/external/llvh/lib/Support/StringExtras.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- StringExtras.cpp - Implement the StringExtras header --------------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // This file implements the StringExtras.h header |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "llvh/ADT/StringExtras.h" |
15 | | #include "llvh/ADT/SmallVector.h" |
16 | | #include "llvh/Support/raw_ostream.h" |
17 | | using namespace llvh; |
18 | | |
19 | | /// StrInStrNoCase - Portable version of strcasestr. Locates the first |
20 | | /// occurrence of string 's1' in string 's2', ignoring case. Returns |
21 | | /// the offset of s2 in s1 or npos if s2 cannot be found. |
22 | 0 | StringRef::size_type llvh::StrInStrNoCase(StringRef s1, StringRef s2) { |
23 | 0 | size_t N = s2.size(), M = s1.size(); |
24 | 0 | if (N > M) |
25 | 0 | return StringRef::npos; |
26 | 0 | for (size_t i = 0, e = M - N + 1; i != e; ++i) |
27 | 0 | if (s1.substr(i, N).equals_lower(s2)) |
28 | 0 | return i; |
29 | 0 | return StringRef::npos; |
30 | 0 | } |
31 | | |
32 | | /// getToken - This function extracts one token from source, ignoring any |
33 | | /// leading characters that appear in the Delimiters string, and ending the |
34 | | /// token at any of the characters that appear in the Delimiters string. If |
35 | | /// there are no tokens in the source string, an empty string is returned. |
36 | | /// The function returns a pair containing the extracted token and the |
37 | | /// remaining tail string. |
38 | | std::pair<StringRef, StringRef> llvh::getToken(StringRef Source, |
39 | 0 | StringRef Delimiters) { |
40 | | // Figure out where the token starts. |
41 | 0 | StringRef::size_type Start = Source.find_first_not_of(Delimiters); |
42 | | |
43 | | // Find the next occurrence of the delimiter. |
44 | 0 | StringRef::size_type End = Source.find_first_of(Delimiters, Start); |
45 | |
|
46 | 0 | return std::make_pair(Source.slice(Start, End), Source.substr(End)); |
47 | 0 | } |
48 | | |
49 | | /// SplitString - Split up the specified string according to the specified |
50 | | /// delimiters, appending the result fragments to the output list. |
51 | | void llvh::SplitString(StringRef Source, |
52 | | SmallVectorImpl<StringRef> &OutFragments, |
53 | 0 | StringRef Delimiters) { |
54 | 0 | std::pair<StringRef, StringRef> S = getToken(Source, Delimiters); |
55 | 0 | while (!S.first.empty()) { |
56 | 0 | OutFragments.push_back(S.first); |
57 | 0 | S = getToken(S.second, Delimiters); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | void llvh::printEscapedString(StringRef Name, raw_ostream &Out) { |
62 | 0 | for (unsigned i = 0, e = Name.size(); i != e; ++i) { |
63 | 0 | unsigned char C = Name[i]; |
64 | 0 | if (isPrint(C) && C != '\\' && C != '"') |
65 | 0 | Out << C; |
66 | 0 | else |
67 | 0 | Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | 0 | void llvh::printHTMLEscaped(StringRef String, raw_ostream &Out) { |
72 | 0 | for (char C : String) { |
73 | 0 | if (C == '&') |
74 | 0 | Out << "&"; |
75 | 0 | else if (C == '<') |
76 | 0 | Out << "<"; |
77 | 0 | else if (C == '>') |
78 | 0 | Out << ">"; |
79 | 0 | else if (C == '\"') |
80 | 0 | Out << """; |
81 | 0 | else if (C == '\'') |
82 | 0 | Out << "'"; |
83 | 0 | else |
84 | 0 | Out << C; |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | 0 | void llvh::printLowerCase(StringRef String, raw_ostream &Out) { |
89 | 0 | for (const char C : String) |
90 | 0 | Out << toLower(C); |
91 | 0 | } |