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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Extracts a character(s) from `subject`.

use stfu8;

#[derive(Clone, Copy, PartialEq)]
enum PointType {
    Length,
    Position,
}

#[derive(Clone, Copy, PartialEq)]
enum CharType {
    Simple,
    Grapheme,
}

fn get_chars(subject: &str, start: usize, end: usize) -> String {
    match subject.len() {
        0 => subject.to_string(),
        _ => crate::split::chars(&subject)[start..end].join(""),
    }
}

fn get_subject_length(
    subject: &str,
    position: usize,
    point_type: PointType,
    char_type: CharType,
) -> usize {
    let subject_len = crate::count::count_graphemes(subject);
    let position_substruction = match point_type {
        PointType::Length => 0,
        PointType::Position => 1,
    };
    let is_out_of_bounds = match char_type {
        CharType::Simple => position > subject_len,
        CharType::Grapheme => position >= subject_len,
    };
    if is_out_of_bounds {
        subject_len - position_substruction
    } else {
        position
    }
}
/// Access a character from `subject` at specified `position`.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `position` - The position to get the character.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::char_at("helicopter", 0);
/// // => "h"
/// chop::char_at("błąd", 1);
/// // => "ł"
/// use voca_rs::Voca;
/// "helicopter"._char_at(0);
/// // => "h"
/// ```
pub fn char_at(subject: &str, position: usize) -> String {
    let the_position = get_subject_length(subject, position, PointType::Position, CharType::Simple);
    get_chars(&subject, the_position, the_position + 1)
}

/// Get the Unicode code point value of the character at `position`.
/// NOTE: Unicode escape must not be a surrogate
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `position` - The position to get the code point number.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::code_point_at("rain", 1);
/// // => [97]
/// chop::code_point_at("cafe\u{0301}", 4);
/// // => [101, 769]
/// use voca_rs::Voca;
/// "rain"._code_point_at(1);
/// // => [97]
/// ```
pub fn code_point_at(subject: &str, position: usize) -> Vec<u16> {
    if subject.is_empty() {
        return vec![];
    }
    let grapheme = grapheme_at(&subject, position);
    crate::split::code_points(&grapheme)
}

/// Extracts the first `length` characters from `subject`.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `length` - The number of characters to extract.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::first("helicopter", 1);
/// // => "h"
/// chop::first("błąd", 2);
/// // => "bł"
/// chop::first("e\u{0301}", 1); // or 'é'
/// // => "e"
/// use voca_rs::Voca;
/// "helicopter"._first(1);
/// // => "h"
/// ```
pub fn first(subject: &str, length: usize) -> String {
    let the_length = get_subject_length(subject, length, PointType::Length, CharType::Simple);
    match length {
        0 => "".to_string(),
        _ => get_chars(&subject, 0, the_length),
    }
}

/// Converts the `subject` to a `foreign_key`.
///
/// # Arguments
///
/// * `subject` - The string to convert to a `foreign_key`.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::foreign_key("foo_bar");
/// // => "foo_bar_id"
/// chop::foreign_key("fooBar3");
/// // => "foo_bar3_id"
/// chop::foreign_key("Test::Foo::Bar");
/// // => "bar_id"
/// use voca_rs::Voca;
/// "foo_bar"._foreign_key();
/// // => "foo_bar_id"
/// ```
pub fn foreign_key(subject: &str) -> String {
    /* https://docs.rs/crate/Inflector/0.11.4 */
    match subject.len() {
        0 => subject.to_string(),
        _ => {
            let safe_string = if subject.contains("::") {
                let split_string: Vec<&str> = subject.split("::").collect();
                split_string[split_string.len() - 1]
            } else {
                subject
            };
            let snake_cased: String = crate::case::snake_case(safe_string);
            if snake_cased.ends_with("_id") {
                snake_cased
            } else {
                format!("{}{}", snake_cased, "_id")
            }
        }
    }
}

/// Get a grapheme from `subject` at specified `position`.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `position` - The position to get the grapheme.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::grapheme_at("cafe\u{0301}", 3); // or 'café'
/// // => "é"
/// chop::grapheme_at("a̐éö̲", 0);
/// // => "a̐"
/// use voca_rs::Voca;
/// "cafe\u{0301}"._grapheme_at(3); // or 'café'
/// // => "é"
/// ```
pub fn grapheme_at(subject: &str, position: usize) -> String {
    let subject_len = crate::count::count_graphemes(subject);
    match subject_len {
        0 => subject.to_string(),
        _ => {
            let the_position =
                get_subject_length(subject, position, PointType::Position, CharType::Grapheme);
            crate::split::graphemes(&subject)[the_position].to_string()
        }
    }
}

/// Extracts the last `length` characters from `subject`.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `length` - The number of characters to extract.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::last("helicopter", 1);
/// // => "r"
/// chop::last("błąd", 2);
/// // => "ąd"
/// chop::last("e\u{0301}", 1); // or 'é'
/// // => "\u{0301}"
/// use voca_rs::Voca;
/// "helicopter"._last(1);
/// // => "r"
/// ```
pub fn last(subject: &str, length: usize) -> String {
    match length {
        0 => "".to_string(),
        _ => {
            let subject_length = crate::split::chars(&subject).len();
            let the_length =
                get_subject_length(subject, length, PointType::Length, CharType::Grapheme);
            get_chars(&subject, subject_length - the_length, subject_length)
        }
    }
}

/// Truncates `subject` to a new `length` and does not break the words. Guarantees that the truncated string is no longer than `length`.
///
/// # Arguments
///
/// * `subject` - The string to prune.
/// * `length` - The length to prune the string.
/// * `end` - The string to be added at the end. Default value is "...".
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::prune("Once upon a time", 7, "");
/// // => "Once..."
/// chop::prune("Die Schildkröte fliegt über das Floß.", 19, "~~");
/// // => "Die Schildkröte~~"
/// chop::prune("Once upon", 10, "");
/// // => "Once upon"
/// chop::prune("Как слышно, приём!", 14, "");
/// // => "Как слышно..."
/// use voca_rs::Voca;
/// "Once upon a time"._prune(7, "");
/// // => "Once..."
/// ```
pub fn prune(subject: &str, length: usize, end: &str) -> String {
    if length == 0 {
        return "".to_string();
    }
    let mut sufix = match end {
        "" => "...",
        _ => end,
    };
    let subject_chars = crate::split::chars(&subject);
    let subject_length = subject_chars.len();
    let end_length = crate::split::chars(&sufix).len();
    let position_end = if subject_length <= length {
        sufix = "";
        subject_length
    } else {
        let string_length = length - end_length;
        let mut char_indices = subject_chars.iter();
        let mut end_position = 0;
        let mut current_position = 0;
        #[derive(Clone, Copy, PartialEq)]
        enum WordMode {
            Spaces,
            Words,
        }
        let mut mode = WordMode::Words;
        while current_position <= string_length {
            let next_char = char_indices.next();
            match next_char {
                Some(c) => {
                    let mut current_char = String::new();
                    current_char.push_str(c);
                    if crate::utils::WHITESPACE.contains(&current_char)
                        || crate::utils::PUNCTUATION.contains(&current_char)
                    {
                        if mode == WordMode::Words {
                            end_position = if current_position > 0 {
                                current_position
                            } else {
                                0
                            };
                            mode = WordMode::Spaces;
                        }
                    } else if mode == WordMode::Spaces {
                        mode = WordMode::Words;
                    }
                }
                None => {
                    return subject.to_string();
                }
            }
            current_position += 1;
        }
        end_position
    };

    format!("{}{}", get_chars(&subject, 0, position_end), sufix)
}

/// Extracts from `subject` a string from `start` position up to `end` position. The character at `end` position is not included.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `start` - The position to start extraction. 0 means extract from the beginning of the `subject`. If negative use `subject.len() - start`.
/// * `end` - The position to end extraction. 0 means extract to the end of the `subject`. If negative use `subject.len() - end`.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::slice("miami", 1, 0);
/// // => "iami"
/// chop::slice("błąd", -2, 0);
/// // => "ąd"
/// chop::slice("florida", 1, 4);
/// // => "lor"
/// chop::slice("e\u{0301}", 1, 0); // or 'é'
/// // => "\u{0301}"
/// chop::slice("Die Schildkröte fliegt.", 4, -8);
/// // => "Schildkröte"
/// use voca_rs::Voca;
/// "miami"._slice(1, 0);
/// // => "iami"
/// ```
pub fn slice(subject: &str, start: isize, end: isize) -> String {
    let subject_length = crate::split::chars(&subject).len();
    let position_start = calculate_position(subject_length, start, true);
    let position_end = calculate_position(subject_length, end, false);

    fn calculate_position(length: usize, x: isize, start: bool) -> usize {
        if x < 0 {
            let pos = length as isize - x.abs();
            if pos < 0 {
                0
            } else {
                pos as usize
            }
        } else if x == 0 {
            if start {
                0
            } else {
                length
            }
        } else if x > length as isize {
            length
        } else {
            x as usize
        }
    }

    get_chars(&subject, position_start, position_end)
}

/// Extracts from `subject` a string from `start` position a number of `length` characters.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `start` - The position to start extraction. 0 means extract from the beginning of the `subject`.
/// * `length` - The number of characters to extract. 0 means extract to the end of `subject`.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::substr("beach", 1, 0);
/// // => "each"
/// chop::substr("błąd", 1, 2);
/// // => "łą"
/// use voca_rs::Voca;
/// "beach"._substr(1, 0);
/// // => "each"
/// ```
pub fn substr(subject: &str, start: usize, length: usize) -> String {
    let subject_length = crate::split::chars(&subject).len();
    if start >= subject_length {
        return "".to_string();
    }
    let position_end = match length {
        0 => subject_length,
        _ => {
            let to_position = start + length;
            if to_position > subject_length {
                subject_length
            } else {
                to_position
            }
        }
    };
    if start >= position_end {
        return "".to_string();
    }
    get_chars(&subject, start, position_end)
}

/// Extracts from `subject` a string from `start` position up to `end` position. The character at `end` position is not included.
///
/// # Arguments
///
/// * `subject` - The string to extract from.
/// * `start` - The position to start extraction. 0 means extract from the beginning of the `subject`.
/// * `end` - The position to end extraction. 0 means extract to the end of the `subject`.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::substring("beach", 1, 0);
/// // => "each"
/// chop::substring("błąd", 2, 4);
/// // => "ąd"
/// chop::substring("e\u{0301}", 1, 0); // or 'é'
/// // => "\u{0301}"
/// use voca_rs::Voca;
/// "beach"._substring(1, 0);
/// // => "each"
/// ```
pub fn substring(subject: &str, start: usize, end: usize) -> String {
    let subject_length = crate::split::chars(&subject).len();
    if start >= subject_length {
        return "".to_string();
    }
    let position_end = match end {
        0 => subject_length,
        _ => {
            if end > subject_length {
                subject_length
            } else {
                end
            }
        }
    };
    if start > position_end {
        return "".to_string();
    }

    get_chars(&subject, start, position_end)
}

/// Truncates `subject` to a new `length`.
///
/// # Arguments
///
/// * `subject` - The string to truncate.
/// * `length` - The length to truncate the string.
/// * `end` - The string to be added at the end. Default value is "...".
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::truncate("Once upon a time", 7, "");
/// // => "Once..."
/// chop::truncate("Die Schildkröte fliegt über das Floß.", 28, "(...)");
/// // => "Die Schildkröte fliegt (...)"
/// chop::truncate("Once upon", 10, "");
/// // => "Once upon"
/// use voca_rs::Voca;
/// "Once upon a time"._truncate(7, "");
/// // => "Once..."
/// ```
pub fn truncate(subject: &str, length: usize, end: &str) -> String {
    if length == 0 {
        return "".to_string();
    }
    let mut sufix = match end {
        "" => "...",
        _ => end,
    };
    let subject_length = crate::split::chars(&subject).len();
    let end_length = crate::split::chars(&sufix).len();
    let position_end = if subject_length < length || length < end_length {
        sufix = "";
        subject_length
    } else {
        length - end_length
    };
    format!("{}{}", get_chars(&subject, 0, position_end), sufix)
}

/// Returns the max character from the `subject` by its code point.
/// NOTE: Unicode escape must not be a surrogate
///
/// # Arguments
///
/// * `subject` - The string to extract from.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::max("rain");
/// // => "r"
/// chop::max("cafe\u{0301}"); // or "café"
/// // => "\u{0301}"
/// chop::max("a̐éö̲"); // or "a\u{310}e\u{301}o\u{308}\u{332}"
/// // => "\u{332}"
/// use voca_rs::Voca;
/// "rain"._max_code_point();
/// // => "r"
/// ```
pub fn max(subject: &str) -> String {
    if subject.is_empty() {
        return "".to_owned();
    }
    min_max(&subject, MinMaxType::Max)
}

/// Returns the min character from the `subject` by its code point.
/// NOTE: Unicode escape must not be a surrogate
///
/// # Arguments
///
/// * `subject` - The string to extract from.
///
/// # Example
/// ```
/// use voca_rs::*;
/// chop::min("rain");
/// // => "a"
/// chop::min("cafe\u{0301}"); // or "café"
/// // => "a"
/// chop::min("Über das Floß.");
/// // => " "
/// use voca_rs::Voca;
/// "rain"._min_code_point();
/// // => "a"
/// ```
pub fn min(subject: &str) -> String {
    if subject.is_empty() {
        return "".to_owned();
    }
    min_max(&subject, MinMaxType::Min)
}

#[derive(Clone, Copy, PartialEq)]
enum MinMaxType {
    Min,
    Max,
}

fn min_max(subject: &str, search_type: MinMaxType) -> String {
    if subject.is_empty() {
        return "".to_owned();
    }
    let code_points = crate::split::code_points(&subject);
    let min_max = match search_type {
        MinMaxType::Max => code_points.iter().max(),
        MinMaxType::Min => code_points.iter().min(),
    };
    match min_max {
        None => "".to_owned(),
        Some(x) => stfu8::encode_u16(&[*x]),
    }
}