forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
case.h
128 lines (114 loc) · 4.47 KB
/
case.h
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
// 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 package contains character classification functions for evaluating
// the case state of strings, and converting strings to uppercase, lowercase,
// etc.
//
// Unlike <ctype.h> (or absl/strings/ascii.h), the functions in this file
// are designed to operate on strings, not single characters.
//
// Except for those marked as "using the C/POSIX locale", these functions are
// for ASCII strings only.
#ifndef OR_TOOLS_BASE_CASE_H_
#define OR_TOOLS_BASE_CASE_H_
#ifndef _MSC_VER
#include <strings.h> // for strcasecmp, but msvc does not have this header
#endif
#include <cstddef>
#include <cstring>
#include <functional>
#include <ostream>
#include <string>
#include "absl/base/attributes.h"
#include "absl/base/macros.h"
#include "absl/base/port.h" // disable some warnings on Windows
#include "absl/strings/ascii.h"
#include "absl/strings/string_view.h"
namespace strings {
// Enum values returned by GetAsciiCapitalization().
enum class AsciiCapitalizationType {
kLower, // Entirely lowercase
kUpper, // Entirely uppercase
kFirst, // First letter uppercase
kMixed, // Mixed case
kNoAlpha // Not an alphabetic string
};
// Prints the name of an enum value.
std::ostream& operator<<(std::ostream& os, const AsciiCapitalizationType& type);
// GetAsciiCapitalization()
//
// Returns a value indicating whether an ASCII string is entirely lowercase,
// entirely uppercase, first letter uppercase, or mixed case, as returned by
// `absl::ascii_islower()` and `absl::ascii_isupper()`.
AsciiCapitalizationType GetAsciiCapitalization(absl::string_view input);
// AsciiCaseInsensitiveCompare()
//
// Performs a case-insensitive absl::string_view comparison.
// Returns:
// less than 0: if s1 < s2
// equal to 0: if s1 == s2
// greater than 0: if s1 > s2
int AsciiCaseInsensitiveCompare(absl::string_view s1, absl::string_view s2);
// AsciiCaseInsensitiveLess()
//
// Performs a case-insensitive less-than absl::string_view comparison. This
// function object is useful as a template parameter for set/map of
// absl::string_view-compatible types, if uniqueness of keys is
// case-insensitive.
// Can be used for heterogeneous lookups in associative containers. Example:
//
// absl::btree_map<std::string, std::string, AsciiCaseInsensitiveLess> map;
// absl::string_view key = ...;
// auto it = map.find(key);
struct AsciiCaseInsensitiveLess {
// Enable heterogeneous lookup.
using is_transparent = void;
bool operator()(absl::string_view s1, absl::string_view s2) const {
return AsciiCaseInsensitiveCompare(s1, s2) < 0;
}
};
// AsciiCaseInsensitiveHash and AsciiCaseInsensitiveEq
//
// Performs a case-insensitive hash/eq absl::string_view operations. This
// function objects are useful as a template parameter for hash set/map of
// absl::string_view-compatible types, if uniqueness of keys is
// case-insensitive.
// Can be used for heterogeneous lookups in absl associative containers.
// Example:
//
// absl::flat_hash_map<std::string, std::string,
// AsciiCaseInsensitiveHash,
// AsciiCaseInsensitiveEq>
// map;
// absl::string_view key = ...;
// auto it = map.find(key);
struct AsciiCaseInsensitiveHash {
using is_transparent = void;
size_t operator()(absl::string_view s) const;
};
struct AsciiCaseInsensitiveEq {
using is_transparent = void;
bool operator()(absl::string_view s1, absl::string_view s2) const;
};
// MakeAsciiTitlecase()
//
// Capitalizes the first character of each word in a string, using the set of
// characters in `delimiters` to use as word boundaries. This function can be
// implemented using a regular expression, but this version should be more
// efficient.
void MakeAsciiTitlecase(std::string* s, absl::string_view delimiters);
// As above but with string_view as input
std::string MakeAsciiTitlecase(absl::string_view s,
absl::string_view delimiters);
} // namespace strings
#endif // OR_TOOLS_BASE_CASE_H_