Debian Codegraph @

packages/seacas/applications/exodiff/stringx.C

raw
1 // Copyright(C) 1999-2021, 2023, 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "smart_assert.h" // for SMART_ASSERT
8 #include "stringx.h"
9
10 #include <cctype> // for tolower, isspace
11 #include <cstring> // for strspn, strcspn
12 #include <string> // for string, operator==
13 #include <vector> // for vector
14
15 bool abbreviation(const std::string &s, const std::string &master, unsigned min_length)
16 {
17 SMART_ASSERT(min_length > 0);
18
19 if (s.size() > master.size()) {
20 return false;
21 }
22
23 if (s.size() < min_length) {
24 return false;
25 }
26
27 for (unsigned i = 0; i < s.size(); ++i) {
28 if (s[i] != master[i]) {
29 return false;
30 }
31 }
32 return true;
33 }
34
35 bool no_case_equals(const std::string &s1, const std::string &s2)
36 {
37 if (s1.size() != s2.size()) {
38 return false;
39 }
40
41 for (unsigned i = 0; i < s1.size(); ++i) {
42 if (tolower(s1[i]) != tolower(s2[i])) {
43 return false;
44 }
45 }
46 return true;
47 }
48
49 std::string &chop_whitespace(std::string &s)
50 {
51 if (!s.empty()) {
52 int i = (int)s.size() - 1;
53 for (; i >= 0; --i) {
54 if (isspace(static_cast<int>(s[i])) == 0) {
55 break;
56 }
57
58 s.resize(i + 1);
59 }
60 }
61 return s;
62 }
63
64 std::string extract_token(std::string &s, const char *delimiters)
65 {
66 if (!s.empty()) {
67 SMART_ASSERT(delimiters != nullptr && !std::string(delimiters).empty());
68
69 // Move through initial delimiters.
70 auto p = s.find_first_not_of(delimiters);
71
72 if (p >= s.size()) {
73 // no tokens
74 s = "";
75 return "";
76 }
77 if (s[p] == '"') {
78 // Special case of a quoted variable name which likely contains
79 // whitespace, but it should work for any quoted variable
80 // name. Some of this is a bit redundant but it makes this section
81 // completely self contained so that this code is only executed
82 // when a quoted variable name is found and it doesn't affect any
83 // action outside of this block of code.
84
85 // Find the closing quote
86 auto cq = s.find_first_of('\"', p + 1);
87
88 // No closing quote found. Error out.
89 SMART_ASSERT(cq < s.size());
90
91 std::string tok = s.substr(p + 1, cq - (p + 1));
92
93 auto r = s.find_first_not_of(delimiters, cq + 1);
94
95 if (r >= s.size()) {
96 s = "";
97 }
98 else {
99 s.erase(0, r);
100 }
101 return tok;
102 }
103
104 // move to end of first token
105 auto q = s.find_first_of(delimiters, p);
106
107 if (q >= s.size()) {
108 // no more delimiters
109 std::string tok = s.substr(p);
110 s = "";
111 return tok;
112 }
113
114 SMART_ASSERT(q > p);
115 std::string tok = s.substr(p, q - p);
116
117 // move to start of the second token
118 auto r = s.find_first_not_of(delimiters, q);
119
120 if (r >= s.size()) {
121 // no second token
122 s = "";
123 }
124 else {
125 s.erase(0, r);
126 }
127
128 return tok;
129 }
130
131 return "";
132 }
133
134 int count_tokens(const std::string &s, const char *delimiters)
135 {
136 if (!s.empty()) {
137 const char *str_ptr = s.c_str();
138
139 // Move through initial delimiters.
140 const char *p = &str_ptr[strspn(str_ptr, delimiters)];
141
142 int num_toks = 0;
143 while (p[0] != '\0') {
144 ++num_toks;
145 p = &p[strcspn(p, delimiters)]; // Move through token.
146 p = &p[strspn(p, delimiters)]; // Move through delimiters.
147 }
148
149 return num_toks;
150 }
151
152 return 0;
153 }
154
155 int max_string_length(const NameList &names)
156 {
157 if (names.empty()) {
158 return 0;
159 }
160 auto len = names[0].size();
161 for (size_t i = 1; i < names.size(); i++) {
162 if (names[i].size() > len) {
163 len = names[i].size();
164 }
165 }
166 return len;
167 }
168
169 void to_lower(std::string &s)
170 {
171 for (auto &elem : s) {
172 elem = (char)tolower(elem);
173 }
174 }
175
176 char first_character(const std::string &s)
177 {
178 for (const auto &elem : s) {
179 if (isspace(static_cast<int>(elem)) == 0) {
180 return elem;
181 }
182 }
183 return 0;
184 }
185
186 int find_string(const NameList &lst, const std::string &s, bool nocase)
187 {
188 if (nocase) {
189 for (unsigned i = 0; i < lst.size(); ++i) {
190 if (no_case_equals(lst[i], s)) {
191 return i;
192 }
193 }
194 }
195 else {
196 for (unsigned i = 0; i < lst.size(); ++i) {
197 if (lst[i] == s) {
198 return i;
199 }
200 }
201 }
202 return -1;
203 }