Skip to content

Commit f8d2558

Browse files
authored
[merge] slicer update
1 parent 0c24295 commit f8d2558

1 file changed

Lines changed: 54 additions & 26 deletions

File tree

src/slicer.rs

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
/*
2-
* MIT License
3-
*
4-
* Copyright (c) 2024 Firelink Data
5-
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
12-
*
13-
* The above copyright notice and this permission notice shall be included in all
14-
* copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-
* SOFTWARE.
23-
*
24-
* File created: 2023-12-11
25-
* Last updated: 2024-05-08
26-
*/
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2024 Firelink Data
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
// File created: 2023-12-11
25+
// Last updated: 2024-05-10
26+
//
27+
2728

2829
use std::default::Default;
2930
use std::slice::Iter;
31+
#[cfg(feature = "nightly")]
32+
use core::str::utf8_char_width;
3033

3134
use crate::error::Result;
3235

@@ -50,6 +53,7 @@ impl Slicer {
5053
///
5154
/// # Panics
5255
/// Iff the byte slice is not a valid utf-8 sequence.
56+
#[cfg(not(feature = "nightly"))]
5357
pub fn find_num_bytes_for_num_runes(&self, bytes: &[u8], num_runes: usize) -> usize {
5458
let mut found_runes: usize = 0;
5559
let mut num_bytes: usize = 0;
@@ -78,6 +82,30 @@ impl Slicer {
7882
num_bytes
7983
}
8084

85+
/// Calculate how many bytes correspond to how many runes in the provided byte slice.
86+
#[cfg(feature = "nightly")]
87+
pub fn find_num_bytes_for_num_runes(&self, bytes: &[u8], num_runes: usize) -> usize {
88+
let mut found_runes: usize = 0;
89+
let mut num_bytes: usize = 0;
90+
let mut byte_units: usize = 1;
91+
92+
let mut iterator: Iter<u8> = bytes.iter();
93+
94+
while found_runes < num_runes {
95+
let byte = match iterator.nth(byte_units - 1) {
96+
Some(b) => *b,
97+
None => break,
98+
};
99+
100+
byte_units = utf8_char_width(byte);
101+
102+
found_runes += 1;
103+
num_bytes += byte_units;
104+
}
105+
106+
num_bytes
107+
}
108+
81109
/// Find all line breaks in a slice of bytes representing utf-8 encoded data.
82110
/// This method looks for a line-feed (LF) character, represented as `\n`.
83111
/// The hexadecimal code for `\n` is 0x0a.

0 commit comments

Comments
 (0)