blob: 3a708ba66475430b356936dff07c9d856684103e [file] [log] [blame]
// 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