OR-Tools  9.6
case.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 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 
14 // This file contains string processing functions related to
15 // uppercase, lowercase, etc.
16 #include "ortools/base/case.h"
17 
18 #include <functional>
19 #include <ostream>
20 #include <string>
21 
22 #include "absl/hash/hash.h"
23 #include "absl/strings/ascii.h"
24 #include "absl/strings/match.h"
25 #include "absl/strings/string_view.h"
26 
27 #ifdef _MSC_VER
28 #define strncasecmp _strnicmp
29 #define strcasecmp _stricmp
30 #endif
31 
32 namespace strings {
33 
34 std::ostream& operator<<(std::ostream& os,
35  const AsciiCapitalizationType& type) {
36  switch (type) {
38  return os << "kLower";
40  return os << "kUpper";
42  return os << "kFirst";
44  return os << "kMixed";
46  return os << "kNoAlpha";
47  default:
48  return os << "INVALID";
49  }
50 }
51 
53  const char* s = input.data();
54  const char* const end = s + input.size();
55  // find the caps type of the first alpha char
56  for (; s != end && !(absl::ascii_isupper(*s) || absl::ascii_islower(*s));
57  ++s) {
58  }
59  if (s == end) return AsciiCapitalizationType::kNoAlpha;
60  const AsciiCapitalizationType firstcapstype =
61  (absl::ascii_islower(*s)) ? AsciiCapitalizationType::kLower
63 
64  // skip ahead to the next alpha char
65  for (++s; s != end && !(absl::ascii_isupper(*s) || absl::ascii_islower(*s));
66  ++s) {
67  }
68  if (s == end) return firstcapstype;
69  const AsciiCapitalizationType capstype =
70  (absl::ascii_islower(*s)) ? AsciiCapitalizationType::kLower
72 
73  if (firstcapstype == AsciiCapitalizationType::kLower &&
76 
77  for (; s != end; ++s)
78  if ((absl::ascii_isupper(*s) &&
79  capstype != AsciiCapitalizationType::kUpper) ||
80  (absl::ascii_islower(*s) &&
83 
84  if (firstcapstype == AsciiCapitalizationType::kUpper &&
87  return capstype;
88 }
89 
90 int AsciiCaseInsensitiveCompare(absl::string_view s1, absl::string_view s2) {
91  if (s1.size() == s2.size()) {
92  return strncasecmp(s1.data(), s2.data(), s1.size());
93  } else if (s1.size() < s2.size()) {
94  int res = strncasecmp(s1.data(), s2.data(), s1.size());
95  return (res == 0) ? -1 : res;
96  } else {
97  int res = strncasecmp(s1.data(), s2.data(), s2.size());
98  return (res == 0) ? 1 : res;
99  }
100 }
101 
102 size_t AsciiCaseInsensitiveHash::operator()(absl::string_view s) const {
103  // return absl::HashOf(absl::AsciiStrToLower(s));
104  return std::hash<std::string>{}(absl::AsciiStrToLower(s));
105 }
106 
107 bool AsciiCaseInsensitiveEq::operator()(absl::string_view s1,
108  absl::string_view s2) const {
109  return s1.size() == s2.size() &&
110  strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
111 }
112 
113 void MakeAsciiTitlecase(std::string* s, absl::string_view delimiters) {
114  bool upper = true;
115  for (auto& ch : *s) {
116  if (upper) {
117  ch = absl::ascii_toupper(ch);
118  }
119  upper = (absl::StrContains(delimiters, ch));
120  }
121 }
122 
123 std::string MakeAsciiTitlecase(absl::string_view s,
124  absl::string_view delimiters) {
125  std::string result(s);
126  MakeAsciiTitlecase(&result, delimiters);
127  return result;
128 }
129 } // namespace strings
double upper
Definition: glpk_solver.cc:82
Definition: case.cc:32
int AsciiCaseInsensitiveCompare(absl::string_view s1, absl::string_view s2)
Definition: case.cc:90
AsciiCapitalizationType GetAsciiCapitalization(const absl::string_view input)
Definition: case.cc:52
AsciiCapitalizationType
Definition: case.h:45
std::ostream & operator<<(std::ostream &os, const AsciiCapitalizationType &type)
Definition: case.cc:34
void MakeAsciiTitlecase(std::string *s, absl::string_view delimiters)
Definition: case.cc:113
static int input(yyscan_t yyscanner)
std::optional< int64_t > end
bool operator()(absl::string_view s1, absl::string_view s2) const
Definition: case.cc:107
size_t operator()(absl::string_view s) const
Definition: case.cc:102