-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.hh
143 lines (101 loc) · 3.42 KB
/
string.hh
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
// Copyright Stephan T. Lavavej, http://nuwen.net .
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://boost.org/LICENSE_1_0.txt .
#ifndef PHAM_STRING_HH
#define PHAM_STRING_HH
#include "compiler.hh"
#ifdef NUWEN_PLATFORM_MSVC
#pragma once
#endif
#include "typedef.hh"
#include "vector.hh"
#include "external_begin.hh"
#include <algorithm>
#include <cctype>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <boost/regex.hpp>
#include "external_end.hh"
namespace nuwen {
inline std::string strip_outside_whitespace(const std::string& s);
inline std::string& find_and_replace_mutate(std::string& s, const std::string& old, const std::string& nu);
inline std::string find_and_replace_copy(const std::string& s, const std::string& old, const std::string& nu);
inline std::string upper(const std::string& s);
inline std::string lower(const std::string& s);
inline std::string comma_from_ull(ull_t n);
inline std::string comma_from_sll(sll_t n);
}
namespace pham {
inline std::string low_three_digits_from_ull(nuwen::ull_t n);
}
inline std::string nuwen::strip_outside_whitespace(const std::string& s) {
static const boost::regex r("\\A\\s*(.*?)\\s*\\z");
return regex_replace(s, r, "$1");
}
inline std::string& nuwen::find_and_replace_mutate(std::string& s, const std::string& old, const std::string& nu) {
std::string t = find_and_replace_copy(s, old, nu);
s.swap(t);
return s;
}
inline std::string nuwen::find_and_replace_copy(const std::string& s, const std::string& old, const std::string& nu) {
using namespace std;
vc_t v;
string::const_iterator i = s.begin();
while (static_cast<string::size_type>(s.end() - i) >= old.size()) {
const string::const_iterator match = search(i, s.end(), old.begin(), old.end());
if (match == s.end()) {
break;
}
copy(i, match, back_inserter(v));
copy(nu.begin(), nu.end(), back_inserter(v));
i = match + old.size();
}
copy(i, s.end(), back_inserter(v));
return string_cast<string>(v);
}
inline std::string nuwen::upper(const std::string& s) {
using namespace std;
string ret(s);
for (string::iterator i = ret.begin(); i != ret.end(); ++i) {
*i = static_cast<char>(toupper(*i));
}
return ret;
}
inline std::string nuwen::lower(const std::string& s) {
using namespace std;
string ret(s);
for (string::iterator i = ret.begin(); i != ret.end(); ++i) {
*i = static_cast<char>(tolower(*i));
}
return ret;
}
inline std::string nuwen::comma_from_ull(ull_t n) {
std::string s = pham::low_three_digits_from_ull(n);
n /= 1000;
while (n != 0) {
s = pham::low_three_digits_from_ull(n) + "," + s;
n /= 1000;
}
return s;
}
inline std::string nuwen::comma_from_sll(const sll_t n) {
if (static_cast<ull_t>(n) == 0x8000000000000000ULL) {
return "-9,223,372,036,854,775,808";
} else if (n < 0) {
return "-" + comma_from_ull(-n);
} else {
return comma_from_ull(n);
}
}
inline std::string pham::low_three_digits_from_ull(const nuwen::ull_t n) {
std::ostringstream oss;
if (n > 999) {
oss.width(3);
oss.fill('0');
}
return static_cast<std::ostringstream &>(oss << n % 1000).str();
}
#endif // Idempotency