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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! This rust crate is for creating a string between two other strings, that is lexicographically
//! halfway between them.
//!
//! ```
//! // "aaa" |
//! // | -> creates "aan"
//! // "aaz" |
//! ```
//!
//! This is useful for re-ordering items in the database with only one update. Get the left and
//! right key strings (that is used for ordering), then create the new key string with
//! `mid_string()` function. Then assign this key string to the new item that is placed in between
//! the previous two items. If you sort by this key, it should be ordered correctly.
//!
//! **Important! For now, this works only for lower case English ascii characters: [a ~ z]. Other characters will result in unpredictable behaviour.**
//!
//! This library is based on the answer provided by "m69" to the following stackoverflow.com question:
//!
//! [https://stackoverflow.com/questions/38923376/return-a-new-string-that-sorts-between-two-given-strings](https://stackoverflow.com/questions/38923376/return-a-new-string-that-sorts-between-two-given-strings)
//!
//! Question: Return a new string that sorts between two given strings
//!
//! Answer provided by "m69 snarky and unwelcoming":
//! [https://stackoverflow.com/a/38927158/1762976](https://stackoverflow.com/a/38927158/1762976)
//!
//! # Examples
//!
//! ```
//! use midstring::mid_string;
//!
//! // create new strings from empty strings
//! println!("_, i => {}", mid_string("", "i")); // -> e
//! println!("_, b => {}", mid_string("", "b")); // -> an
//! println!("_, _ => {}", mid_string("", "")); // -> n
//!
//! assert_eq!(mid_string("", "i"), "e");
//! assert_eq!(mid_string("", "b"), "an");
//! assert_eq!(mid_string("", ""), "n");
//!
//! // create new strings between other strings
//! assert_eq!(mid_string("aaa", "aaz"), "aan");
//! assert_eq!(mid_string("abc", "abcab"), "abcaan");
//!
//! assert_eq!(mid_string(&String::from("abcde"), "abchi"), String::from("abcf"));
//!
//! let left = String::from("abc");
//! let right = "abcab".to_string();
//! let should_be = String::from("abcaan");
//! assert_eq!(mid_string(&left, &right), should_be);
//! ```
//!
// The original C code provided by m69:
// https://stackoverflow.com/a/38927158/1762976
/*
int midstring(const char *prev, const char *next, char *buf) {
char p = 0, n = 0;
int len = 0;
while (p == n) { // copy identical part
p = prev[len] ? prev[len] : 'a' - 1;
n = next[len] ? next[len] : 'z' + 1;
if (p == n) buf[len++] = p;
}
if (p == 'a' - 1) { // end of left string
while (n == 'a') { // handle a's
buf[len++] = 'a';
n = next[len] ? next[len] : 'z' + 1;
}
if (n == 'b') { // handle b
buf[len++] = 'a';
n = 'z' + 1;
}
}
else if (p + 1 == n) { // consecutive characters
n = 'z' + 1;
buf[len++] = p;
while ((p = prev[len] ? prev[len] : 'a' - 1) == 'z') { // handle z's
buf[len++] = 'z';
}
}
buf[len++] = n - (n - p) / 2; // append middle character
buf[len] = '\0';
return len;
}
*/
///
/// Some constants for representing ascii characters
///
const A: u8 = 0x61; // 'a'
const B: u8 = 0x62; // 'b'
const _N: u8 = 0x6E; // 'n'
const Z: u8 = 0x7A; // 'z'
/// Create a string that is lexicographically halfway between the left and right strings
///
/// # Examples
///
/// ```
/// use midstring::mid_string;
///
/// assert_eq!(mid_string("aaa", "aaz"), "aan");
/// assert_eq!(mid_string("abc", "abcab"), "abcaan");
///
/// assert_eq!(mid_string(&String::from("abcde"), "abchi"), String::from("abcf"));
///
/// let left = String::from("abc");
/// let right = "abcab".to_string();
/// let should_be = String::from("abcaan");
/// assert_eq!(mid_string(&left, &right), should_be);
/// ```
///
/// The original code provided by "m69 snarky and unwelcoming"