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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// ---------------- [ File: bitcoin-string/src/capitalize.rs ]
crate::ix!();
/// Capitalize the first character of the provided ASCII string.
///
/// Only bytes in the standard 7‑bit ASCII range are affected; the function is
/// locale‑independent.
pub fn capitalize(str_: &str) -> String {
trace!("capitalize: input = {}", str_);
if str_.is_empty() {
return str_.to_owned();
}
let mut bytes = str_.as_bytes().to_vec();
if let Some(first) = bytes.get_mut(0) {
if (*first >= b'a') && (*first <= b'z') {
*first = *first - b'a' + b'A';
}
}
// SAFETY: the original input is valid UTF‑8 and we only mutate byte 0 in
// the ASCII range, so the result is still valid UTF‑8.
String::from_utf8(bytes).expect("ASCII is valid UTF‑8")
}
impl ToLower for String {
/**
| Returns the lowercase equivalent of
| the given string.
|
| This function is locale independent.
| It only converts uppercase characters
| in the standard 7-bit ASCII range.
|
| This is a feature, not a limitation.
|
| -----------
| @param[in] str
|
| the string to convert to lowercase.
|
| -----------
| @return
|
| lowercased equivalent of str
|
*/
fn to_lower(&self) -> String {
trace!("to_lower(String): input = {}", self);
let mut out = String::with_capacity(self.len());
for &byte in self.as_bytes() {
let lowered = if (byte >= b'A') && (byte <= b'Z') {
byte - b'A' + b'a'
} else {
byte
};
out.push(lowered as char);
}
out
}
}
impl ToUpper for String {
/**
| Returns the uppercase equivalent of
| the given string.
|
| This function is locale independent.
| It only converts lowercase characters
| in the standard 7-bit ASCII range.
|
| This is a feature, not a limitation.
|
| -----------
| @param[in] str
|
| the string to convert to uppercase.
|
| -----------
| @return
|
| UPPERCASED EQUIVALENT OF str
|
*/
fn to_upper(&self) -> String {
trace!("to_upper(String): input = {}", self);
let mut out = String::with_capacity(self.len());
for &byte in self.as_bytes() {
let uppered = if (byte >= b'a') && (byte <= b'z') {
byte - b'a' + b'A'
} else {
byte
};
out.push(uppered as char);
}
out
}
}
impl ToLower for u8 {
/**
| Converts the given character to its
| lowercase equivalent.
|
| This function is locale independent.
| It only converts uppercase characters
| in the standard 7-bit ASCII range.
|
| This is a feature, not a limitation.
|
| -----------
| @param[in] c
|
| the character to convert to lowercase.
|
| -----------
| @return
|
| the lowercase equivalent of c; or the
| argument if no conversion is possible.
|
*/
#[inline]
fn to_lower(&self) -> u8 {
let c = *self;
if (c >= b'A') && (c <= b'Z') {
c - b'A' + b'a'
} else {
c
}
}
}
impl ToUpper for u8 {
/**
| Converts the given character to its
| uppercase equivalent.
|
| This function is locale independent.
| It only converts lowercase characters
| in the standard 7-bit ASCII range.
|
| This is a feature, not a limitation.
|
| -----------
| @param[in] c
|
| the character to convert to uppercase.
|
| -----------
| @return
|
| the uppercase equivalent of c; or the
| argument if no conversion is possible.
|
*/
#[inline]
fn to_upper(&self) -> u8 {
let c = *self;
if (c >= b'a') && (c <= b'z') {
c - b'a' + b'A'
} else {
c
}
}
}
#[cfg(test)]
mod tests_case_conversion {
use super::*;
#[traced_test]
fn capitalize_basic_ascii() {
assert_eq!(capitalize("hello"), "Hello");
}
#[traced_test]
fn capitalize_non_alpha_initial() {
assert_eq!(capitalize("1world"), "1world");
}
#[traced_test]
fn string_to_lower() {
assert_eq!("HeLLo123".to_string().to_lower(), "hello123");
}
#[traced_test]
fn string_to_upper() {
assert_eq!("HeLLo123".to_string().to_upper(), "HELLO123");
}
#[traced_test]
fn byte_case_roundtrip() {
let upper_a: u8 = b'A';
assert_eq!(upper_a.to_lower().to_upper(), upper_a);
}
}