Skip to content

Commit

Permalink
refactor: extract text chunk (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n authored Nov 4, 2023
1 parent b1895bb commit 8bd953e
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 314 deletions.
16 changes: 5 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/loro-internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ serde-wasm-bindgen = { version = "0.5.0", optional = true }
js-sys = { version = "0.3.60", optional = true }
serde_json = { version = "1" }
arref = "0.1.0"
debug-log = { version = "0.2.1", features = [] }
debug-log = { version = "0.2.2", features = [] }
serde_columnar = { version = "0.3.2" }
tracing = { version = "0.1.37" }
append-only-bytes = { version = "0.1.12", features = ["u32_range"] }
Expand Down
14 changes: 4 additions & 10 deletions crates/loro-internal/fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 5 additions & 17 deletions crates/loro-internal/src/container/richtext/query_by_len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use std::marker::PhantomData;

use generic_btree::{BTreeTrait, FindResult, Query};

use crate::utils::utf16::count_utf16_chars;

use super::richtext_state::{
unicode_to_utf16_index, RichtextStateChunk, RichtextTreeTrait,
};
use super::richtext_state::{RichtextStateChunk, RichtextTreeTrait};

/// An easy way to implement [Query] by using key index
///
Expand Down Expand Up @@ -175,21 +171,13 @@ impl Query<RichtextTreeTrait> for EntityIndexQueryWithEventIndex {
fn confirm_elem(&mut self, _q: &Self::QueryArg, elem: &RichtextStateChunk) -> (usize, bool) {
let left = self.left;
match elem {
RichtextStateChunk::Text { unicode_len, text } => {
if *unicode_len as usize >= left {
self.event_index += if cfg!(feature = "wasm") {
unicode_to_utf16_index(std::str::from_utf8(text).unwrap(), left).unwrap()
} else {
left
};
RichtextStateChunk::Text(s) => {
if s.len() as usize >= left {
self.event_index += s.convert_unicode_offset_to_event_offset(left);
return (left, true);
}

self.event_index += if cfg!(feature = "wasm") {
count_utf16_chars(text)
} else {
*unicode_len as usize
};
self.event_index += s.event_len() as usize;
(left, false)
}
RichtextStateChunk::Style { .. } => {
Expand Down
Loading

0 comments on commit 8bd953e

Please sign in to comment.