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