/Users/deen/code/yugabyte-db/src/yb/util/string_case.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/string_case.h" |
34 | | |
35 | | #include <algorithm> |
36 | | |
37 | | #include <glog/logging.h> |
38 | | |
39 | | namespace yb { |
40 | | |
41 | | using std::string; |
42 | | |
43 | | void SnakeToCamelCase(const std::string &snake_case, |
44 | 3 | std::string *camel_case) { |
45 | 0 | DCHECK_NE(camel_case, &snake_case) << "Does not support in-place operation"; |
46 | 3 | camel_case->clear(); |
47 | 3 | camel_case->reserve(snake_case.size()); |
48 | | |
49 | 3 | bool uppercase_next = true; |
50 | 20 | for (char c : snake_case) { |
51 | 20 | if ((c == '_') || |
52 | 19 | (c == '-')) { |
53 | 2 | uppercase_next = true; |
54 | 2 | continue; |
55 | 2 | } |
56 | 18 | if (uppercase_next) { |
57 | 5 | camel_case->push_back(toupper(c)); |
58 | 13 | } else { |
59 | 13 | camel_case->push_back(c); |
60 | 13 | } |
61 | 18 | uppercase_next = false; |
62 | 18 | } |
63 | 3 | } |
64 | | |
65 | | void AllCapsToCamelCase(const std::string &all_caps, |
66 | 3 | std::string *camel_case) { |
67 | 0 | DCHECK_NE(camel_case, &all_caps) << "Does not support in-place operation"; |
68 | 3 | camel_case->clear(); |
69 | 3 | camel_case->reserve(all_caps.size()); |
70 | | |
71 | 3 | bool uppercase_next = true; |
72 | 20 | for (char c : all_caps) { |
73 | 20 | if ((c == '_') || |
74 | 19 | (c == '-')) { |
75 | 2 | uppercase_next = true; |
76 | 2 | continue; |
77 | 2 | } |
78 | 18 | if (uppercase_next) { |
79 | 5 | camel_case->push_back(c); |
80 | 13 | } else { |
81 | 13 | camel_case->push_back(tolower(c)); |
82 | 13 | } |
83 | 18 | uppercase_next = false; |
84 | 18 | } |
85 | 3 | } |
86 | | |
87 | 0 | std::string AllCapsToCamelCase(const std::string &all_caps) { |
88 | 0 | std::string sresult; |
89 | 0 | AllCapsToCamelCase(all_caps, &sresult); |
90 | 0 | return sresult; |
91 | 0 | } |
92 | | |
93 | | void ToLowerCase(const std::string &string, |
94 | 3.92k | std::string *out) { |
95 | 3.92k | if (out != &string) { |
96 | 3.92k | *out = string; |
97 | 3.92k | } |
98 | | |
99 | 49.8k | for (char& c : *out) { |
100 | 49.8k | c = tolower(c); |
101 | 49.8k | } |
102 | 3.92k | } |
103 | | |
104 | | void ToUpperCase(const std::string &string, |
105 | 17.6k | std::string *out) { |
106 | 17.6k | if (out != &string) { |
107 | 17.6k | *out = string; |
108 | 17.6k | } |
109 | | |
110 | 292k | for (char& c : *out) { |
111 | 292k | c = toupper(c); |
112 | 292k | } |
113 | 17.6k | } |
114 | | |
115 | 24 | void Capitalize(string *word) { |
116 | 24 | auto size = word->size(); |
117 | 24 | if (size == 0) { |
118 | 0 | return; |
119 | 0 | } |
120 | | |
121 | 24 | (*word)[0] = toupper((*word)[0]); |
122 | | |
123 | 166 | for (size_t i = 1; i < size; i++) { |
124 | 142 | (*word)[i] = tolower((*word)[i]); |
125 | 142 | } |
126 | 24 | } |
127 | | |
128 | 62 | bool ContainsUpperCase(const string& str) { |
129 | 62 | return std::any_of(str.begin(), str.end(), isupper); |
130 | 62 | } |
131 | | |
132 | | } // namespace yb |