forked from UWQuickstep/quickstep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtil.cpp
158 lines (139 loc) · 4 KB
/
StringUtil.cpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
**/
#define __STDC_FORMAT_MACROS
#include "utility/StringUtil.hpp"
#include <algorithm>
#include <cctype>
#include <cinttypes>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include "glog/logging.h"
// NetBSD's libc has snprintf, but it doesn't show up in the std namespace
// for c++.
#ifndef __NetBSD__
using std::snprintf;
#endif
using std::string;
namespace quickstep {
std::string ToLower(const std::string& str) {
std::string lower_str(str.size(), ' ');
std::transform(str.begin(), str.end(), lower_str.begin(), tolower);
return lower_str;
}
std::string EscapeSpecialChars(const std::string& text) {
std::string new_text;
for (const char& c : text) {
switch (c) {
case '\'':
new_text.append("\\\'");
break;
case '\"':
new_text.append("\\\"");
break;
case '\?':
new_text.append("\\\?");
break;
case '\t':
new_text.append("\\t");
break;
case '\a':
new_text.append("\\a");
break;
case '\b':
new_text.append("\\b");
break;
case '\f':
new_text.append("\\f");
break;
case '\n':
new_text.append("\\n");
break;
case '\r':
new_text.append("\\r");
break;
case '\v':
new_text.append("\\v");
break;
default:
new_text += c;
}
}
return new_text;
}
std::string EscapeQuotes(const std::string &str, const char quote) {
std::string ret;
for (const char c : str) {
ret.push_back(c);
if (c == quote) {
ret.push_back(c);
}
}
return ret;
}
bool ParseIntString(const std::string &int_string,
const char delimiter,
std::vector<int> *parsed_output) {
std::vector<int>::size_type original_size = parsed_output->size();
std::size_t pos = 0;
while (pos < int_string.size()) {
char *endptr = nullptr;
int element = std::strtol(int_string.c_str() + pos, &endptr, 10);
if ((endptr > int_string.c_str() + pos)
&& ((*endptr == delimiter) || (*endptr == '\0'))) {
parsed_output->push_back(element);
pos = endptr - int_string.c_str() + 1;
} else {
parsed_output->resize(original_size);
return false;
}
}
return true;
}
string ToZeroPaddedString(std::uint64_t val, int pad_width) {
DCHECK_GT(pad_width, 0);
const std::size_t str_len = pad_width + 1;
char *str = new char[str_len]();
int bytes = snprintf(str,
str_len,
"%0*" PRIu64,
pad_width,
val);
DCHECK_EQ(pad_width, bytes);
string result(str, pad_width);
delete[] str;
return result;
}
std::string
DoubleToStringWithSignificantDigits(double val,
std::uint64_t significant_digits) {
std::uint64_t precision_needed =
(val >= 1.0) ? significant_digits
: significant_digits + -(std::trunc(std::log10(val)));
std::stringstream string_buffer;
string_buffer << std::fixed << std::setprecision(precision_needed) << val;
return string_buffer.str();
}
} // namespace quickstep