mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
rename flowy str iterator
This commit is contained in:
parent
85ad90fc51
commit
53126c47d1
@ -150,17 +150,17 @@ where
|
|||||||
return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
|
return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
|
||||||
}
|
}
|
||||||
let mut new_s = String::new();
|
let mut new_s = String::new();
|
||||||
let utf16_iter = &mut s.utf16_iter();
|
let code_point_iter = &mut s.code_point_iter();
|
||||||
for op in &self.ops {
|
for op in &self.ops {
|
||||||
match &op {
|
match &op {
|
||||||
Operation::Retain(retain) => {
|
Operation::Retain(retain) => {
|
||||||
for c in utf16_iter.take(retain.n as usize) {
|
for c in code_point_iter.take(retain.n as usize) {
|
||||||
new_s.push_str(&c);
|
new_s.push_str(str::from_utf8(c.0).unwrap_or(""));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Operation::Delete(delete) => {
|
Operation::Delete(delete) => {
|
||||||
for _ in 0..*delete {
|
for _ in 0..*delete {
|
||||||
utf16_iter.next();
|
code_point_iter.next();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Operation::Insert(insert) => {
|
Operation::Insert(insert) => {
|
||||||
@ -187,7 +187,7 @@ where
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Operation::Insert(insert) => {
|
Operation::Insert(insert) => {
|
||||||
inverted.delete(insert.count_of_code_units());
|
inverted.delete(insert.count_of_utf16_code_units());
|
||||||
},
|
},
|
||||||
Operation::Delete(delete) => {
|
Operation::Delete(delete) => {
|
||||||
inverted.insert(&chars.take(*delete as usize).collect::<String>(), op.get_attributes());
|
inverted.insert(&chars.take(*delete as usize).collect::<String>(), op.get_attributes());
|
||||||
@ -294,12 +294,12 @@ where
|
|||||||
(Some(Operation::Insert(insert)), _) => {
|
(Some(Operation::Insert(insert)), _) => {
|
||||||
// let composed_attrs = transform_attributes(&next_op1, &next_op2, true);
|
// let composed_attrs = transform_attributes(&next_op1, &next_op2, true);
|
||||||
a_prime.insert(&insert.s, insert.attributes.clone());
|
a_prime.insert(&insert.s, insert.attributes.clone());
|
||||||
b_prime.retain(insert.count_of_code_units(), insert.attributes.clone());
|
b_prime.retain(insert.count_of_utf16_code_units(), insert.attributes.clone());
|
||||||
next_op1 = ops1.next();
|
next_op1 = ops1.next();
|
||||||
},
|
},
|
||||||
(_, Some(Operation::Insert(o_insert))) => {
|
(_, Some(Operation::Insert(o_insert))) => {
|
||||||
let composed_attrs = transform_op_attribute(&next_op1, &next_op2)?;
|
let composed_attrs = transform_op_attribute(&next_op1, &next_op2)?;
|
||||||
a_prime.retain(o_insert.count_of_code_units(), composed_attrs.clone());
|
a_prime.retain(o_insert.count_of_utf16_code_units(), composed_attrs.clone());
|
||||||
b_prime.insert(&o_insert.s, composed_attrs);
|
b_prime.insert(&o_insert.s, composed_attrs);
|
||||||
next_op2 = ops2.next();
|
next_op2 = ops2.next();
|
||||||
},
|
},
|
||||||
|
@ -181,7 +181,7 @@ where
|
|||||||
Operation::<T>::Insert(insert) => {
|
Operation::<T>::Insert(insert) => {
|
||||||
tracing::trace!("extend insert attributes with {} ", &insert.attributes);
|
tracing::trace!("extend insert attributes with {} ", &insert.attributes);
|
||||||
attributes.extend_other(insert.attributes.clone());
|
attributes.extend_other(insert.attributes.clone());
|
||||||
length = insert.count_of_code_units();
|
length = insert.count_of_utf16_code_units();
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,12 @@ use std::{fmt, fmt::Formatter, slice};
|
|||||||
pub struct FlowyStr(pub String);
|
pub struct FlowyStr(pub String);
|
||||||
|
|
||||||
impl FlowyStr {
|
impl FlowyStr {
|
||||||
|
// https://stackoverflow.com/questions/2241348/what-is-unicode-utf-8-utf-16
|
||||||
pub fn count_utf16_code_units(&self) -> usize { count_utf16_code_units(&self.0) }
|
pub fn count_utf16_code_units(&self) -> usize { count_utf16_code_units(&self.0) }
|
||||||
|
|
||||||
pub fn utf16_iter(&self) -> FlowyUtf16Iterator { FlowyUtf16Iterator::new(self, 0) }
|
pub fn utf16_iter(&self) -> FlowyUtf16CodePointIterator { FlowyUtf16CodePointIterator::new(self, 0) }
|
||||||
|
|
||||||
pub fn code_point_iter(&self) -> CodePointIterator { CodePointIterator::new(self) }
|
pub fn code_point_iter(&self) -> Utf16CodeUnitIterator { Utf16CodeUnitIterator::new(self) }
|
||||||
|
|
||||||
pub fn sub_str(&self, interval: Interval) -> String {
|
pub fn sub_str(&self, interval: Interval) -> String {
|
||||||
match self.with_interval(interval) {
|
match self.with_interval(interval) {
|
||||||
@ -19,7 +20,7 @@ impl FlowyStr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_interval(&self, interval: Interval) -> Option<FlowyStr> {
|
pub fn with_interval(&self, interval: Interval) -> Option<FlowyStr> {
|
||||||
let mut iter = CodePointIterator::new(self);
|
let mut iter = Utf16CodeUnitIterator::new(self);
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
while let Some((byte, _len)) = iter.next() {
|
while let Some((byte, _len)) = iter.next() {
|
||||||
if interval.start < iter.code_point_offset && interval.end >= iter.code_point_offset {
|
if interval.start < iter.code_point_offset && interval.end >= iter.code_point_offset {
|
||||||
@ -38,7 +39,7 @@ impl FlowyStr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CodePointIterator<'a> {
|
pub struct Utf16CodeUnitIterator<'a> {
|
||||||
s: &'a FlowyStr,
|
s: &'a FlowyStr,
|
||||||
bytes_offset: usize,
|
bytes_offset: usize,
|
||||||
code_point_offset: usize,
|
code_point_offset: usize,
|
||||||
@ -46,9 +47,9 @@ pub struct CodePointIterator<'a> {
|
|||||||
iter: slice::Iter<'a, u8>,
|
iter: slice::Iter<'a, u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CodePointIterator<'a> {
|
impl<'a> Utf16CodeUnitIterator<'a> {
|
||||||
pub fn new(s: &'a FlowyStr) -> Self {
|
pub fn new(s: &'a FlowyStr) -> Self {
|
||||||
CodePointIterator {
|
Utf16CodeUnitIterator {
|
||||||
s,
|
s,
|
||||||
bytes_offset: 0,
|
bytes_offset: 0,
|
||||||
code_point_offset: 0,
|
code_point_offset: 0,
|
||||||
@ -58,7 +59,7 @@ impl<'a> CodePointIterator<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for CodePointIterator<'a> {
|
impl<'a> Iterator for Utf16CodeUnitIterator<'a> {
|
||||||
type Item = (&'a [u8], usize);
|
type Item = (&'a [u8], usize);
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
@ -168,19 +169,19 @@ impl<'de> Deserialize<'de> for FlowyStr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FlowyUtf16Iterator<'a> {
|
pub struct FlowyUtf16CodePointIterator<'a> {
|
||||||
s: &'a FlowyStr,
|
s: &'a FlowyStr,
|
||||||
offset: usize,
|
offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FlowyUtf16Iterator<'a> {
|
impl<'a> FlowyUtf16CodePointIterator<'a> {
|
||||||
pub fn new(s: &'a FlowyStr, offset: usize) -> Self { FlowyUtf16Iterator { s, offset } }
|
pub fn new(s: &'a FlowyStr, offset: usize) -> Self { FlowyUtf16CodePointIterator { s, offset } }
|
||||||
}
|
}
|
||||||
|
|
||||||
use crate::core::Interval;
|
use crate::core::Interval;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
impl<'a> Iterator for FlowyUtf16Iterator<'a> {
|
impl<'a> Iterator for FlowyUtf16CodePointIterator<'a> {
|
||||||
type Item = String;
|
type Item = String;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
@ -67,7 +67,7 @@ where
|
|||||||
match self {
|
match self {
|
||||||
Operation::Delete(n) => *n,
|
Operation::Delete(n) => *n,
|
||||||
Operation::Retain(r) => r.n,
|
Operation::Retain(r) => r.n,
|
||||||
Operation::Insert(i) => i.count_of_code_units(),
|
Operation::Insert(i) => i.count_of_utf16_code_units(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +95,7 @@ where
|
|||||||
.build(),
|
.build(),
|
||||||
);
|
);
|
||||||
right = Some(
|
right = Some(
|
||||||
OpBuilder::<T>::insert(&insert.s[index..insert.count_of_code_units()])
|
OpBuilder::<T>::insert(&insert.s[index..insert.count_of_utf16_code_units()])
|
||||||
.attributes(attributes)
|
.attributes(attributes)
|
||||||
.build(),
|
.build(),
|
||||||
);
|
);
|
||||||
@ -112,7 +112,7 @@ where
|
|||||||
.attributes(retain.attributes.clone())
|
.attributes(retain.attributes.clone())
|
||||||
.build(),
|
.build(),
|
||||||
Operation::Insert(insert) => {
|
Operation::Insert(insert) => {
|
||||||
if interval.start > insert.count_of_code_units() {
|
if interval.start > insert.count_of_utf16_code_units() {
|
||||||
OpBuilder::insert("").build()
|
OpBuilder::insert("").build()
|
||||||
} else {
|
} else {
|
||||||
// let s = &insert
|
// let s = &insert
|
||||||
@ -291,7 +291,7 @@ impl<T> Insert<T>
|
|||||||
where
|
where
|
||||||
T: Attributes,
|
T: Attributes,
|
||||||
{
|
{
|
||||||
pub fn count_of_code_units(&self) -> usize { self.s.count_utf16_code_units() }
|
pub fn count_of_utf16_code_units(&self) -> usize { self.s.count_utf16_code_units() }
|
||||||
|
|
||||||
pub fn merge_or_new_op(&mut self, s: &str, attributes: T) -> Option<Operation<T>> {
|
pub fn merge_or_new_op(&mut self, s: &str, attributes: T) -> Option<Operation<T>> {
|
||||||
if self.attributes == attributes {
|
if self.attributes == attributes {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user