OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "Renamer.h" | |
6 | |
7 #include <assert.h> | |
8 | |
9 #include "clang/Basic/CharInfo.h" | |
10 #include "llvm/ADT/SmallVector.h" | |
11 | |
12 namespace { | |
13 | |
14 using WordList = llvm::SmallVector<llvm::StringRef, 6>; | |
15 | |
16 WordList SplitIdentifier(llvm::StringRef input) { | |
17 assert(input.size() > 0); | |
18 | |
19 WordList output; | |
20 for (size_t i = 0; i < input.size(); ) { | |
21 size_t start = i; | |
22 size_t end = input.size(); | |
23 bool was_lowercase = clang::isLowercase(input[start]); | |
24 bool was_uppercase = clang::isUppercase(input[start]); | |
25 bool in_acronym = false; | |
26 | |
27 // Find the next "meaningful" uppercase letter or underscore. | |
28 for (++i; i < input.size(); ++i) { | |
29 | |
30 // Underscore case: assume underscores always separate two words. | |
31 if (input[i] == '_') { | |
32 end = i; | |
33 ++i; // Skip the underscore. | |
34 break; | |
35 } | |
36 | |
37 bool is_lowercase = clang::isLowercase(input[i]); | |
38 bool is_uppercase = clang::isUppercase(input[i]); | |
39 // Non-acronym case: the next uppercase letter is the beginning of the | |
40 // next word. | |
41 if (was_lowercase && is_uppercase) { | |
danakj
2016/01/26 21:32:10
What about V8Stuff?
8 won't be uppercase or lower
dcheng
2016/01/27 00:09:28
I made a small tweak so this gets handled correctl
| |
42 end = i; | |
43 break; | |
44 } | |
45 // End of ACRONYM case: | |
46 if (in_acronym && was_uppercase && is_lowercase) { | |
47 --i; // Already iterated into the next word, so backtrack. | |
danakj
2016/01/26 21:32:10
Should it in_acronym = false?
dcheng
2016/01/27 00:09:28
This is scoped to the body of the outermost for lo
| |
48 end = i; | |
49 break; | |
50 } | |
51 // Seeing two consecutive uppercase letters indicates that this is | |
52 // probably part of an acronym. | |
danakj
2016/01/26 21:32:10
Heh, except AThing. I wonder if that happens..
It
dcheng
2016/01/27 00:09:28
This is OK though: we'll mark this as being in an
| |
53 if (was_uppercase && is_uppercase) { | |
54 in_acronym = true; | |
55 // Intentionally fallthrough, so casing state is updated. | |
56 } | |
57 | |
58 was_lowercase = is_lowercase; | |
59 was_uppercase = is_uppercase; | |
60 } | |
61 // TODO(dcheng): Special acronym handling here. | |
62 output.emplace_back(input.substr(start, end - start)); | |
63 } | |
64 | |
65 return output; | |
66 } | |
67 | |
68 } // namespace | |
69 | |
70 std::string IdentifierToCamelCase(llvm::StringRef input) { | |
71 WordList words = SplitIdentifier(input); | |
72 std::string output; | |
73 for (const auto& word : words) { | |
74 output += clang::toUppercase(word[0]); | |
75 for (size_t i = 1; i < word.size(); ++i) | |
76 output += clang::toLowercase(word[i]); | |
77 } | |
78 return output; | |
79 } | |
80 | |
81 std::string IdentifierToUnderscoreCase(llvm::StringRef input) { | |
82 WordList words = SplitIdentifier(input); | |
83 std::string output; | |
84 for (const auto& word : words) { | |
85 if (!output.empty()) | |
86 output += '_'; | |
87 for (size_t i = 0; i < word.size(); ++i) | |
88 output += clang::toLowercase(word[i]); | |
89 } | |
90 return output; | |
91 } | |
92 | |
93 std::string identifierToShoutyCase(llvm::StringRef input) { | |
94 WordList words = SplitIdentifier(input); | |
95 std::string output; | |
96 for (const auto& word : words) { | |
97 if (!output.empty()) | |
98 output += '_'; | |
99 for (size_t i = 0; i < word.size(); ++i) | |
100 output += clang::toUppercase(word[i]); | |
101 } | |
102 return output; | |
103 } | |
OLD | NEW |