char-positions
Similar to the standard library's .char_indicies(), but instead of only
producing the start byte position. This library implements .char_positions(),
which can produce any combination of line, column, start byte, and end byte position.
As an example use
text.char_positions::<LineCol>()
to get the line and column of each char.
Use LineColByteRange to additionally get the byte range,
or just Line to simply get the line number.
Example
use ;
let text = "Hello 👋\nWorld 🌏\n🦀🦀";
for in text.char_positions
Which will output:
[Ln 1, Col 1] 'H'
[Ln 1, Col 2] 'e'
[Ln 1, Col 3] 'l'
[Ln 1, Col 4] 'l'
[Ln 1, Col 5] 'o'
[Ln 1, Col 6] ' '
[Ln 1, Col 7] '👋'
[Ln 1, Col 8] '\n'
[Ln 2, Col 1] 'W'
[Ln 2, Col 2] 'o'
[Ln 2, Col 3] 'r'
[Ln 2, Col 4] 'l'
[Ln 2, Col 5] 'd'
[Ln 2, Col 6] ' '
[Ln 2, Col 7] '🌏'
[Ln 2, Col 8] '\n'
[Ln 3, Col 1] '🦀'
[Ln 3, Col 2] '🦀'
Supported
.char_positions::<T>() |
Produces |
|---|---|
usize |
Start byte index (same as .char_indicies()) |
std::ops::Range<usize> |
Start byte and end byte index, i.e. &text[range] is the char |
LineColByteRange |
Line number, column number, and byte range |
LineCol |
Line number and column number |
Line |
Line number |
Col |
Column number |
ByteRange |
Same as using std::ops::Range<usize> |
ByteStart |
Start byte index (same as .char_indicies()) |
ByteEnd |
End byte index |
| Tuples are also supported, e.g. | |
| (Line,) | Produces the tuple |
| (Line, Col) | Produces the tuple |
| (Line, Col, ByteStart, ByteEnd) | Produces the tuple |
| etc. |
Example - LineColByteRange
use ;
let text = "Hello 👋\nWorld 🌏\n🦀🦀";
let mut iter = text
.
.map| );
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;