forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
case.cc
129 lines (113 loc) · 4.2 KB
/
case.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2010-2024 Google LLC
// 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.
// This file contains string processing functions related to
// uppercase, lowercase, etc.
#include "ortools/base/case.h"
#include <functional>
#include <ostream>
#include <string>
#include "absl/hash/hash.h"
#include "absl/strings/ascii.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#ifdef _MSC_VER
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
#endif
namespace strings {
std::ostream& operator<<(std::ostream& os,
const AsciiCapitalizationType& type) {
switch (type) {
case AsciiCapitalizationType::kLower:
return os << "kLower";
case AsciiCapitalizationType::kUpper:
return os << "kUpper";
case AsciiCapitalizationType::kFirst:
return os << "kFirst";
case AsciiCapitalizationType::kMixed:
return os << "kMixed";
case AsciiCapitalizationType::kNoAlpha:
return os << "kNoAlpha";
default:
return os << "INVALID";
}
}
AsciiCapitalizationType GetAsciiCapitalization(const absl::string_view input) {
const char* s = input.data();
const char* const end = s + input.size();
// find the caps type of the first alpha char
for (; s != end && !(absl::ascii_isupper(*s) || absl::ascii_islower(*s));
++s) {
}
if (s == end) return AsciiCapitalizationType::kNoAlpha;
const AsciiCapitalizationType firstcapstype =
(absl::ascii_islower(*s)) ? AsciiCapitalizationType::kLower
: AsciiCapitalizationType::kUpper;
// skip ahead to the next alpha char
for (++s; s != end && !(absl::ascii_isupper(*s) || absl::ascii_islower(*s));
++s) {
}
if (s == end) return firstcapstype;
const AsciiCapitalizationType capstype =
(absl::ascii_islower(*s)) ? AsciiCapitalizationType::kLower
: AsciiCapitalizationType::kUpper;
if (firstcapstype == AsciiCapitalizationType::kLower &&
capstype == AsciiCapitalizationType::kUpper)
return AsciiCapitalizationType::kMixed;
for (; s != end; ++s)
if ((absl::ascii_isupper(*s) &&
capstype != AsciiCapitalizationType::kUpper) ||
(absl::ascii_islower(*s) &&
capstype != AsciiCapitalizationType::kLower))
return AsciiCapitalizationType::kMixed;
if (firstcapstype == AsciiCapitalizationType::kUpper &&
capstype == AsciiCapitalizationType::kLower)
return AsciiCapitalizationType::kFirst;
return capstype;
}
int AsciiCaseInsensitiveCompare(absl::string_view s1, absl::string_view s2) {
if (s1.size() == s2.size()) {
return strncasecmp(s1.data(), s2.data(), s1.size());
} else if (s1.size() < s2.size()) {
int res = strncasecmp(s1.data(), s2.data(), s1.size());
return (res == 0) ? -1 : res;
} else {
int res = strncasecmp(s1.data(), s2.data(), s2.size());
return (res == 0) ? 1 : res;
}
}
size_t AsciiCaseInsensitiveHash::operator()(absl::string_view s) const {
// return absl::HashOf(absl::AsciiStrToLower(s));
return std::hash<std::string>{}(absl::AsciiStrToLower(s));
}
bool AsciiCaseInsensitiveEq::operator()(absl::string_view s1,
absl::string_view s2) const {
return s1.size() == s2.size() &&
strncasecmp(s1.data(), s2.data(), s1.size()) == 0;
}
void MakeAsciiTitlecase(std::string* s, absl::string_view delimiters) {
bool upper = true;
for (auto& ch : *s) {
if (upper) {
ch = absl::ascii_toupper(ch);
}
upper = (absl::StrContains(delimiters, ch));
}
}
std::string MakeAsciiTitlecase(absl::string_view s,
absl::string_view delimiters) {
std::string result(s);
MakeAsciiTitlecase(&result, delimiters);
return result;
}
} // namespace strings