-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is intended for bits of code shared between various backend implementations. closes #183
- Loading branch information
Showing
3 changed files
with
46 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
pub use kurbo; | ||
|
||
/// utilities shared by various backends | ||
pub mod util; | ||
|
||
mod color; | ||
mod conv; | ||
mod error; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! Code useful for multiple backends | ||
/// Counts the number of utf-16 code units in the given string. | ||
/// from xi-editor | ||
pub fn count_utf16(s: &str) -> usize { | ||
let mut utf16_count = 0; | ||
for &b in s.as_bytes() { | ||
if (b as i8) >= -0x40 { | ||
utf16_count += 1; | ||
} | ||
if b >= 0xf0 { | ||
utf16_count += 1; | ||
} | ||
} | ||
utf16_count | ||
} | ||
|
||
/// returns utf8 text position (code unit offset) at the given utf-16 text position | ||
#[allow(clippy::explicit_counter_loop)] | ||
pub fn count_until_utf16(s: &str, utf16_text_position: usize) -> Option<usize> { | ||
let mut utf8_count = 0; | ||
let mut utf16_count = 0; | ||
for &b in s.as_bytes() { | ||
if (b as i8) >= -0x40 { | ||
utf16_count += 1; | ||
} | ||
if b >= 0xf0 { | ||
utf16_count += 1; | ||
} | ||
|
||
if utf16_count > utf16_text_position { | ||
return Some(utf8_count); | ||
} | ||
|
||
utf8_count += 1; | ||
} | ||
|
||
None | ||
} |