OR-Tools  9.2
case.h
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 package contains character classification functions for evaluating
15 // the case state of strings, and converting strings to uppercase, lowercase,
16 // etc.
17 //
18 // Unlike <ctype.h> (or absl/strings/ascii.h), the functions in this file
19 // are designed to operate on strings, not single characters.
20 //
21 // Except for those marked as "using the C/POSIX locale", these functions are
22 // for ASCII strings only.
23 
24 #ifndef OR_TOOLS_BASE_CASE_H_
25 #define OR_TOOLS_BASE_CASE_H_
26 
27 #ifndef _MSC_VER
28 #include <strings.h> // for strcasecmp, but msvc does not have this header
29 #endif
30 
31 #include <cstddef>
32 #include <cstring>
33 #include <functional>
34 #include <ostream>
35 #include <string>
36 
37 #include "absl/base/attributes.h"
38 #include "absl/base/macros.h"
39 #include "absl/base/port.h" // disable some warnings on Windows
40 #include "absl/strings/ascii.h"
41 #include "absl/strings/string_view.h"
42 
43 namespace strings {
44 // Enum values returned by GetAsciiCapitalization().
46  kLower, // Entirely lowercase
47  kUpper, // Entirely uppercase
48  kFirst, // First letter uppercase
49  kMixed, // Mixed case
50  kNoAlpha // Not an alphabetic string
51 };
52 
53 // Prints the name of an enum value.
54 std::ostream& operator<<(std::ostream& os, const AsciiCapitalizationType& type);
55 
56 // GetAsciiCapitalization()
57 //
58 // Returns a value indicating whether an ASCII string is entirely lowercase,
59 // entirely uppercase, first letter uppercase, or mixed case, as returned by
60 // `absl::ascii_islower()` and `absl::ascii_isupper()`.
62 
63 // AsciiCaseInsensitiveCompare()
64 //
65 // Performs a case-insensitive absl::string_view comparison.
66 // Returns:
67 // less than 0: if s1 < s2
68 // equal to 0: if s1 == s2
69 // greater than 0: if s1 > s2
70 int AsciiCaseInsensitiveCompare(absl::string_view s1, absl::string_view s2);
71 
72 // AsciiCaseInsensitiveLess()
73 //
74 // Performs a case-insensitive less-than absl::string_view comparison. This
75 // function object is useful as a template parameter for set/map of
76 // absl::string_view-compatible types, if uniqueness of keys is
77 // case-insensitive.
78 // Can be used for heterogeneous lookups in associative containers. Example:
79 //
80 // absl::btree_map<std::string, std::string, AsciiCaseInsensitiveLess> map;
81 // absl::string_view key = ...;
82 // auto it = map.find(key);
84  // Enable heterogeneous lookup.
85  using is_transparent = void;
86  bool operator()(absl::string_view s1, absl::string_view s2) const {
87  return AsciiCaseInsensitiveCompare(s1, s2) < 0;
88  }
89 };
90 
91 // AsciiCaseInsensitiveHash and AsciiCaseInsensitiveEq
92 //
93 // Performs a case-insensitive hash/eq absl::string_view operations. This
94 // function objects are useful as a template parameter for hash set/map of
95 // absl::string_view-compatible types, if uniqueness of keys is
96 // case-insensitive.
97 // Can be used for heterogeneous lookups in absl associative containers.
98 // Example:
99 //
100 // absl::flat_hash_map<std::string, std::string,
101 // AsciiCaseInsensitiveHash,
102 // AsciiCaseInsensitiveEq>
103 // map;
104 // absl::string_view key = ...;
105 // auto it = map.find(key);
107  using is_transparent = void;
108  size_t operator()(absl::string_view s) const;
109 };
111  using is_transparent = void;
112  bool operator()(absl::string_view s1, absl::string_view s2) const;
113 };
114 
115 // MakeAsciiTitlecase()
116 //
117 // Capitalizes the first character of each word in a string, using the set of
118 // characters in `delimiters` to use as word boundaries. This function can be
119 // implemented using a regular expression, but this version should be more
120 // efficient.
121 void MakeAsciiTitlecase(std::string* s, absl::string_view delimiters);
122 
123 // As above but with string_view as input
124 std::string MakeAsciiTitlecase(absl::string_view s,
125  absl::string_view delimiters);
126 
127 } // namespace strings
128 #endif // OR_TOOLS_BASE_CASE_H_
void MakeAsciiTitlecase(std::string *s, absl::string_view delimiters)
Definition: case.cc:112
Definition: case.cc:31
std::ostream & operator<<(std::ostream &os, const AsciiCapitalizationType &type)
Definition: case.cc:33
bool operator()(absl::string_view s1, absl::string_view s2) const
Definition: case.h:86
bool operator()(absl::string_view s1, absl::string_view s2) const
Definition: case.cc:106
AsciiCapitalizationType
Definition: case.h:45
size_t operator()(absl::string_view s) const
Definition: case.cc:101
static int input(yyscan_t yyscanner)
AsciiCapitalizationType GetAsciiCapitalization(const absl::string_view input)
Definition: case.cc:51
int AsciiCaseInsensitiveCompare(absl::string_view s1, absl::string_view s2)
Definition: case.cc:89