forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaming_utils.cc
68 lines (52 loc) · 2.16 KB
/
naming_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/strings/naming_utils.h"
#include <algorithm>
#include "absl/strings/ascii.h"
#include "absl/strings/string_view.h"
namespace verible {
bool IsNameAllCapsUnderscoresDigits(absl::string_view text) {
return std::all_of(text.begin(), text.end(), [](char c) {
return absl::ascii_isupper(c) || c == '_' || absl::ascii_isdigit(c);
});
}
bool AllUnderscoresFollowedByDigits(absl::string_view text) {
if (text.empty()) return true;
// Return false if the underscore is the last character.
if (text[text.length() - 1] == '_') return false;
for (absl::string_view::size_type i = 0; i < text.length() - 1; ++i) {
if (text[i] == '_' && !absl::ascii_isdigit(text[i + 1])) return false;
}
return true;
}
bool IsUpperCamelCaseWithDigits(absl::string_view text) {
if (text.empty()) return true;
// Check that the first letter is capital. Not allowing "_foo" cases.
if (!absl::ascii_isupper(text[0])) return false;
// Check for underscores followed by digits
auto pos = text.find('_');
if (pos != absl::string_view::npos)
return AllUnderscoresFollowedByDigits(text.substr(pos));
return true;
}
bool IsLowerSnakeCaseWithDigits(absl::string_view text) {
if (text.empty()) return true;
// Check that the first letter is lowercase. Not allowing "_foo" cases.
if (!absl::ascii_islower(text[0])) return false;
// Check for anything that is not a lowercase letter, underscore, or digit.
return std::all_of(text.begin(), text.end(), [](char c) {
return absl::ascii_islower(c) || c == '_' || absl::ascii_isdigit(c);
});
}
} // namespace verible