Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
lp_name.cc
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
15
16#include "absl/status/status.h"
17#include "absl/strings/ascii.h"
18#include "absl/strings/string_view.h"
20
22
23bool ValidateCharInName(unsigned char c, bool leading) {
24 if (absl::ascii_isalpha(c)) {
25 return true;
26 }
27 if (!leading) {
28 if (c == '.' || absl::ascii_isdigit(c)) {
29 return true;
30 }
31 }
32 switch (c) {
33 case '!':
34 case '"':
35 case '#':
36 case '$':
37 case '%':
38 case '&':
39 case '(':
40 case ')':
41 case ',':
42 case ';':
43 case '?':
44 case '@':
45 case '_':
46 case '`':
47 case '\'':
48 case '{':
49 case '}':
50 case '~':
51 return true;
52 default:
53 return false;
54 }
55}
56
57absl::Status ValidateName(absl::string_view name) {
58 if (name.empty()) {
59 return absl::InvalidArgumentError("empty name invalid");
60 }
61 for (int i = 0; i < name.size(); ++i) {
62 if (!ValidateCharInName(name[i], i == 0)) {
64 << "invalid character: " << name[i] << " at index: " << i
65 << " in: " << name;
66 }
67 }
68 return absl::OkStatus();
69}
70
71} // namespace operations_research::lp_format
const std::string name
A name for logging purposes.
absl::Status ValidateName(absl::string_view name)
Checks if name is a valid name for a variable or constraint in an LP file.
Definition lp_name.cc:57
bool ValidateCharInName(unsigned char c, bool leading)
Definition lp_name.cc:23
StatusBuilder InvalidArgumentErrorBuilder()