23 using namespace basis;
33 astring string_manipulation::make_random_name(
int min,
int max)
39 for (
int i = 0; i < length; i++) {
42 char to_add =
'a' + chah;
43 if (chah == 26) to_add =
'_';
50 astring string_manipulation::long_line(
char line_item,
int repeat)
51 {
return astring(line_item, repeat); }
53 astring string_manipulation::indentation(
int spaces)
56 for (
int i = 0; i < spaces; i++) s +=
' ';
60 void string_manipulation::carriage_returns_to_spaces(
astring &to_strip)
62 for (
int j = 0; j < to_strip.
length(); j++) {
66 if ( (to_strip[j] ==
'\r') && (to_strip[j + 1] ==
'\n') ) {
79 to_strip[original_j] =
'\n';
80 for (
int k = original_j + 1; k < j; k++) to_strip[k] =
' ';
88 for (
int k = original_j; k < j; k++) to_strip[k] =
' ';
95 int min_column,
int max_column)
98 if (max_column - min_column + 1 < 2)
return;
101 carriage_returns_to_spaces(input);
103 int col = min_column;
104 astring indent_add = indentation(min_column);
107 bool just_had_break =
false;
109 bool put_accum_before_break =
false;
115 for (
int j = 0; j < input.
length(); j++) {
123 if (just_had_break) {
124 if (put_accum_before_break) {
125 output += accumulated;
128 output += parser_bits::platform_eol_to_chars();
134 output += parser_bits::platform_eol_to_chars();
136 output += indent_add;
137 just_had_break =
false;
138 if (accumulated.
length()) {
139 output += accumulated;
140 col += accumulated.
length();
147 put_accum_before_break =
false;
150 while ( (input[j] ==
' ') || (input[j] ==
'\t') ) {
152 if (j >= input.
length())
break;
155 if (j >= input.
length())
break;
158 char current_char = input[j];
160 just_had_break =
true;
161 put_accum_before_break =
true;
167 bool add_dash =
false;
168 bool break_line =
false;
169 bool invisible =
false;
170 bool end_sentence =
false;
171 bool punctuate =
false;
172 bool keep_on_line =
false;
173 char prior_break =
'\0';
174 char prior_break_plus_1 =
'\0';
180 next_break = input.
length() - 1;
184 prior_break = input[next_break];
186 prior_break_plus_1 = input[next_break + 1];
188 switch (prior_break) {
189 case '\r':
case '\n':
191 just_had_break =
true;
192 put_accum_before_break =
true;
198 case '?':
case '!':
case '.':
201 while ( (input[next_break + 1] ==
'?')
202 || (input[next_break + 1] ==
'!')
203 || (input[next_break + 1] ==
'.') ) {
207 if (!parser_bits::white_space(input[next_break + 1]))
208 end_sentence =
false;
210 case ',':
case ';':
case ':':
213 if (!parser_bits::white_space(input[next_break + 1]))
220 if (punctuate || invisible) punct_adder = 1;
221 if (end_sentence) punct_adder = 2;
224 int chars_added = next_break - j + 1;
225 if (col + chars_added + punct_adder > max_column) {
228 just_had_break =
true;
229 if (col + chars_added <= max_column) {
234 end_sentence =
false;
237 }
else if (min_column + chars_added > max_column) {
239 int chars_left = max_column - col + 1;
241 if (chars_left < 2) {
245 next_break = j + chars_left - 2;
246 chars_added = next_break - j + 1;
247 if (next_break >= input.
length())
248 next_break = input.
length() - 1;
249 else if (next_break < j)
262 if (add_dash || keep_on_line) {
264 output += adding_chunk;
265 if (add_dash) output +=
"-";
271 accumulated = adding_chunk;
272 if (punctuate || invisible) {
274 }
else if (end_sentence) {
282 output += adding_chunk;
283 col += chars_added + punct_adder;
285 just_had_break =
false;
288 if (punctuate || invisible) {
290 }
else if (end_sentence) {
295 if (accumulated.
length()) {
297 output += parser_bits::platform_eol_to_chars();
298 output += indent_add;
299 output += accumulated;
302 output += parser_bits::platform_eol_to_chars();
305 char string_manipulation::hex_to_char(
abyte to_convert)
307 if (to_convert <= 9)
return char(
'0' + to_convert);
308 else if ( (to_convert >= 10) && (to_convert <= 15) )
309 return char(
'A' - 10 + to_convert);
313 abyte string_manipulation::char_to_hex(
char to_convert)
315 if ( (to_convert >=
'0') && (to_convert <=
'9') )
316 return char(to_convert -
'0');
317 else if ( (to_convert >=
'a') && (to_convert <=
'f') )
318 return char(to_convert -
'a' + 10);
319 else if ( (to_convert >=
'A') && (to_convert <=
'F') )
320 return char(to_convert -
'A' + 10);
327 for (
int i = 0; i < to_convert.
length() / 2; i++) {
328 int str_index = i * 2;
329 abyte first_byte = char_to_hex(to_convert.
get(str_index));
330 abyte second_byte = char_to_hex(to_convert.
get(str_index + 1));
331 abyte to_stuff =
abyte(first_byte * 16 + second_byte);
340 for (
int i = 0; i < to_convert.
length() * 2; i += 2) {
341 int str_index = i / 2;
342 char first_char = hex_to_char(
char(to_convert.
get(str_index) / 16));
343 char second_char = hex_to_char(
char(to_convert.
get(str_index) % 16));
344 to_return +=
astring(first_char, 1);
345 to_return +=
astring(second_char, 1);
array & concatenate(const array &to_concatenate)
Appends the array "to_concatenate" onto "this" and returns "this".
const contents & get(int index) const
Accesses individual objects stored in "this" at the "index" position.
int length() const
Returns the current reported length of the allocated C array.
Provides a dynamically resizable ASCII character string.
virtual char get(int index) const
a constant peek at the string's internals at the specified index.
bool substring(astring &target, int start, int end) const
a version that stores the substring in an existing "target" string.
void strip_spaces(how_to_strip way=FROM_BOTH_SIDES)
removes excess space characters from string's beginning, end or both.
int find_any(const char *to_find, int position=0, bool reverse=false) const
searches for any of the characters in "to_find".
int length() const
Returns the current length of the string.
A very common template for a dynamic array of bytes.
a platform-independent way to acquire random numbers in a specific range.
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
#define NULL_POINTER
The value representing a pointer to nothing.
The guards collection helps in testing preconditions and reporting errors.
unsigned char abyte
A fairly important unit which is seldom defined...
bool negative(const type &a)
negative returns true if "a" is less than zero.
An extension to floating point primitives providing approximate equality.
None split_lines(str unsplit_line)
const char * splitter_finding_set
bool is_eol(char to_check)