From 57a95825d9b5f0f1934146a479f9298c245a29ca Mon Sep 17 00:00:00 2001 From: appflowy Date: Mon, 1 Aug 2022 11:32:25 +0800 Subject: [PATCH] chore: rename flowyStr to OTString --- .../tests/editor/attribute_test.rs | 4 +- .../flowy-text-block/tests/editor/mod.rs | 2 +- .../flowy-text-block/tests/editor/op_test.rs | 8 +- shared-lib/flowy-sync/src/util.rs | 6 +- shared-lib/lib-ot/src/core/delta/delta.rs | 12 +-- shared-lib/lib-ot/src/core/flowy_str.rs | 88 +++++++++---------- .../lib-ot/src/core/operation/operation.rs | 12 +-- .../src/core/operation/operation_serde.rs | 6 +- 8 files changed, 69 insertions(+), 69 deletions(-) diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs b/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs index bdeb1601b9..c59d85e025 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/attribute_test.rs @@ -1,7 +1,7 @@ #![cfg_attr(rustfmt, rustfmt::skip)] use crate::editor::{TestBuilder, TestOp::*}; use flowy_sync::client_document::{NewlineDoc, PlainDoc}; -use lib_ot::core::{Interval, OperationTransform, NEW_LINE, WHITESPACE, FlowyStr}; +use lib_ot::core::{Interval, OperationTransform, NEW_LINE, WHITESPACE, OTString}; use unicode_segmentation::UnicodeSegmentation; use lib_ot::rich_text::RichTextDelta; @@ -723,7 +723,7 @@ fn attributes_preserve_header_format_on_merge() { #[test] fn attributes_format_emoji() { let emoji_s = "πŸ‘‹ "; - let s: FlowyStr = emoji_s.into(); + let s: OTString = emoji_s.into(); let len = s.utf16_len(); assert_eq!(3, len); assert_eq!(2, s.graphemes(true).count()); diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs b/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs index 64f432a1e7..46442a6336 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/mod.rs @@ -300,7 +300,7 @@ impl Rng { pub fn gen_delta(&mut self, s: &str) -> RichTextDelta { let mut delta = RichTextDelta::default(); - let s = FlowyStr::from(s); + let s = OTString::from(s); loop { let left = s.utf16_len() - delta.utf16_base_len; if left == 0 { diff --git a/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs b/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs index cb7bb0a728..ac091398b5 100644 --- a/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs +++ b/frontend/rust-lib/flowy-text-block/tests/editor/op_test.rs @@ -333,7 +333,7 @@ fn sequence() { fn apply_1000() { for _ in 0..1 { let mut rng = Rng::default(); - let s: FlowyStr = rng.gen_string(50).into(); + let s: OTString = rng.gen_string(50).into(); let delta = rng.gen_delta(&s); assert_eq!(s.utf16_len(), delta.utf16_base_len); } @@ -458,16 +458,16 @@ fn compose() { let mut rng = Rng::default(); let s = rng.gen_string(20); let a = rng.gen_delta(&s); - let after_a: FlowyStr = a.apply(&s).unwrap().into(); + let after_a: OTString = a.apply(&s).unwrap().into(); assert_eq!(a.utf16_target_len, after_a.utf16_len()); let b = rng.gen_delta(&after_a); - let after_b: FlowyStr = b.apply(&after_a).unwrap().into(); + let after_b: OTString = b.apply(&after_a).unwrap().into(); assert_eq!(b.utf16_target_len, after_b.utf16_len()); let ab = a.compose(&b).unwrap(); assert_eq!(ab.utf16_target_len, b.utf16_target_len); - let after_ab: FlowyStr = ab.apply(&s).unwrap().into(); + let after_ab: OTString = ab.apply(&s).unwrap().into(); assert_eq!(after_b, after_ab); } } diff --git a/shared-lib/flowy-sync/src/util.rs b/shared-lib/flowy-sync/src/util.rs index 92728af8b1..9c0ab822c8 100644 --- a/shared-lib/flowy-sync/src/util.rs +++ b/shared-lib/flowy-sync/src/util.rs @@ -7,7 +7,7 @@ use crate::{ errors::{CollaborateError, CollaborateResult}, }; use dissimilar::Chunk; -use lib_ot::core::{DeltaBuilder, FlowyStr}; +use lib_ot::core::{DeltaBuilder, OTString}; use lib_ot::{ core::{Attributes, Delta, OperationTransform, NEW_LINE, WHITESPACE}, rich_text::RichTextDelta, @@ -208,10 +208,10 @@ pub fn cal_diff(old: String, new: String) -> Option> { for chunk in &chunks { match chunk { Chunk::Equal(s) => { - delta_builder = delta_builder.retain(FlowyStr::from(*s).utf16_len()); + delta_builder = delta_builder.retain(OTString::from(*s).utf16_len()); } Chunk::Delete(s) => { - delta_builder = delta_builder.delete(FlowyStr::from(*s).utf16_len()); + delta_builder = delta_builder.delete(OTString::from(*s).utf16_len()); } Chunk::Insert(s) => { delta_builder = delta_builder.insert(*s); diff --git a/shared-lib/lib-ot/src/core/delta/delta.rs b/shared-lib/lib-ot/src/core/delta/delta.rs index 53cdcaac54..b047091815 100644 --- a/shared-lib/lib-ot/src/core/delta/delta.rs +++ b/shared-lib/lib-ot/src/core/delta/delta.rs @@ -1,7 +1,7 @@ use crate::errors::{ErrorBuilder, OTError, OTErrorCode}; use crate::core::delta::{DeltaIterator, MAX_IV_LEN}; -use crate::core::flowy_str::FlowyStr; +use crate::core::flowy_str::OTString; use crate::core::interval::Interval; use crate::core::operation::{Attributes, Operation, OperationTransform, PhantomAttributes}; use bytes::Bytes; @@ -118,7 +118,7 @@ where /// Creating a [Insert] operation with string, [s]. pub fn insert(&mut self, s: &str, attributes: T) { - let s: FlowyStr = s.into(); + let s: OTString = s.into(); if s.is_empty() { return; } @@ -189,7 +189,7 @@ where /// assert_eq!("hello, AppFlowy", &after_b); /// ``` pub fn apply(&self, applied_str: &str) -> Result { - let applied_str: FlowyStr = applied_str.into(); + let applied_str: OTString = applied_str.into(); if applied_str.utf16_len() != self.utf16_base_len { return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength) .msg(format!( @@ -200,7 +200,7 @@ where .build()); } let mut new_s = String::new(); - let code_point_iter = &mut applied_str.utf16_code_unit_iter(); + let code_point_iter = &mut applied_str.utf16_iter(); for op in &self.ops { match &op { Operation::Retain(retain) => { @@ -246,8 +246,8 @@ where /// pub fn invert_str(&self, inverted_s: &str) -> Self { let mut inverted = Delta::default(); - let inverted_s: FlowyStr = inverted_s.into(); - let code_point_iter = &mut inverted_s.utf16_code_unit_iter(); + let inverted_s: OTString = inverted_s.into(); + let code_point_iter = &mut inverted_s.utf16_iter(); for op in &self.ops { match &op { diff --git a/shared-lib/lib-ot/src/core/flowy_str.rs b/shared-lib/lib-ot/src/core/flowy_str.rs index 162557f5f3..e6811f190c 100644 --- a/shared-lib/lib-ot/src/core/flowy_str.rs +++ b/shared-lib/lib-ot/src/core/flowy_str.rs @@ -1,41 +1,41 @@ use serde::{de, de::Visitor, Deserialize, Deserializer, Serialize, Serializer}; use std::{fmt, fmt::Formatter}; +/// [OTString] uses [String] as its inner container. #[derive(Clone, Debug, Eq, PartialEq)] -pub struct FlowyStr(pub String); +pub struct OTString(pub String); -impl FlowyStr { +impl OTString { + /// Returns the number of UTF-16 code units in this string. /// - /// # Arguments - /// - /// * `delta`: The delta you want to iterate over. - /// * `interval`: The range for the cursor movement. + /// The length of strings behaves differently in different languages. For example: [Dart] string's + /// length is calculated with UTF-16 code units. The method [utf16_len] returns the length of a + /// String in UTF-16 code units. /// /// # Examples /// /// ``` - /// use lib_ot::core::FlowyStr; - /// let utf16_len = FlowyStr::from("πŸ‘‹").utf16_len(); + /// use lib_ot::core::OTString; + /// let utf16_len = OTString::from("πŸ‘‹").utf16_len(); /// assert_eq!(utf16_len, 2); /// let bytes_len = String::from("πŸ‘‹").len(); /// assert_eq!(bytes_len, 4); /// /// ``` - /// https://stackoverflow.com/questions/2241348/what-is-unicode-utf-8-utf-16 pub fn utf16_len(&self) -> usize { count_utf16_code_units(&self.0) } - pub fn utf16_code_unit_iter(&self) -> Utf16CodeUnitIterator { + pub fn utf16_iter(&self) -> Utf16CodeUnitIterator { Utf16CodeUnitIterator::new(self) } - /// Return a new string with the given [Interval] + /// Returns a new string with the given [Interval] /// # Examples /// /// ``` - /// use lib_ot::core::{FlowyStr, Interval}; - /// let s: FlowyStr = "δ½ ε₯½\n😁".into(); + /// use lib_ot::core::{OTString, Interval}; + /// let s: OTString = "δ½ ε₯½\n😁".into(); /// assert_eq!(s.utf16_len(), 5); /// let output1 = s.sub_str(Interval::new(0, 2)).unwrap(); /// assert_eq!(output1, "δ½ ε₯½"); @@ -69,21 +69,21 @@ impl FlowyStr { /// # Examples /// /// ``` - /// use lib_ot::core::FlowyStr; - /// let s: FlowyStr = "πŸ‘‹πŸ˜πŸ‘‹".into(); /// + /// use lib_ot::core::OTString; + /// let s: OTString = "πŸ‘‹πŸ˜πŸ‘‹".into(); /// /// let mut iter = s.utf16_code_point_iter(); /// assert_eq!(iter.next().unwrap(), "πŸ‘‹".to_string()); /// assert_eq!(iter.next().unwrap(), "😁".to_string()); /// assert_eq!(iter.next().unwrap(), "πŸ‘‹".to_string()); /// assert_eq!(iter.next(), None); /// - /// let s: FlowyStr = "πŸ‘‹12abδΈ€δΊŒπŸ‘‹".into(); /// + /// let s: OTString = "πŸ‘‹12abδΈ€δΊŒπŸ‘‹".into(); /// /// let mut iter = s.utf16_code_point_iter(); /// assert_eq!(iter.next().unwrap(), "πŸ‘‹".to_string()); /// assert_eq!(iter.next().unwrap(), "1".to_string()); /// assert_eq!(iter.next().unwrap(), "2".to_string()); /// - /// assert_eq!(iter.skip(FlowyStr::from("abδΈ€δΊŒ").utf16_len()).next().unwrap(), "πŸ‘‹".to_string()); + /// assert_eq!(iter.skip(OTString::from("abδΈ€δΊŒ").utf16_len()).next().unwrap(), "πŸ‘‹".to_string()); /// ``` #[allow(dead_code)] pub fn utf16_code_point_iter(&self) -> FlowyUtf16CodePointIterator { @@ -91,7 +91,7 @@ impl FlowyStr { } } -impl std::ops::Deref for FlowyStr { +impl std::ops::Deref for OTString { type Target = String; fn deref(&self) -> &Self::Target { @@ -99,46 +99,46 @@ impl std::ops::Deref for FlowyStr { } } -impl std::ops::DerefMut for FlowyStr { +impl std::ops::DerefMut for OTString { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } -impl std::convert::From for FlowyStr { +impl std::convert::From for OTString { fn from(s: String) -> Self { - FlowyStr(s) + OTString(s) } } -impl std::convert::From<&str> for FlowyStr { +impl std::convert::From<&str> for OTString { fn from(s: &str) -> Self { s.to_owned().into() } } -impl std::fmt::Display for FlowyStr { +impl std::fmt::Display for OTString { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str(&self.0) } } -impl std::ops::Add<&str> for FlowyStr { - type Output = FlowyStr; +impl std::ops::Add<&str> for OTString { + type Output = OTString; - fn add(self, rhs: &str) -> FlowyStr { + fn add(self, rhs: &str) -> OTString { let new_value = self.0 + rhs; new_value.into() } } -impl std::ops::AddAssign<&str> for FlowyStr { +impl std::ops::AddAssign<&str> for OTString { fn add_assign(&mut self, rhs: &str) { self.0 += rhs; } } -impl Serialize for FlowyStr { +impl Serialize for OTString { fn serialize(&self, serializer: S) -> Result where S: Serializer, @@ -147,15 +147,15 @@ impl Serialize for FlowyStr { } } -impl<'de> Deserialize<'de> for FlowyStr { - fn deserialize(deserializer: D) -> Result +impl<'de> Deserialize<'de> for OTString { + fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - struct FlowyStrVisitor; + struct OTStringVisitor; - impl<'de> Visitor<'de> for FlowyStrVisitor { - type Value = FlowyStr; + impl<'de> Visitor<'de> for OTStringVisitor { + type Value = OTString; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter.write_str("a str") @@ -168,19 +168,19 @@ impl<'de> Deserialize<'de> for FlowyStr { Ok(s.into()) } } - deserializer.deserialize_str(FlowyStrVisitor) + deserializer.deserialize_str(OTStringVisitor) } } pub struct Utf16CodeUnitIterator<'a> { - s: &'a FlowyStr, + s: &'a OTString, byte_offset: usize, utf16_offset: usize, utf16_count: usize, } impl<'a> Utf16CodeUnitIterator<'a> { - pub fn new(s: &'a FlowyStr) -> Self { + pub fn new(s: &'a OTString) -> Self { Utf16CodeUnitIterator { s, byte_offset: 0, @@ -219,12 +219,12 @@ impl<'a> Iterator for Utf16CodeUnitIterator<'a> { } pub struct FlowyUtf16CodePointIterator<'a> { - s: &'a FlowyStr, + s: &'a OTString, offset: usize, } impl<'a> FlowyUtf16CodePointIterator<'a> { - pub fn new(s: &'a FlowyStr, offset: usize) -> Self { + pub fn new(s: &'a OTString, offset: usize) -> Self { FlowyUtf16CodePointIterator { s, offset } } } @@ -278,15 +278,15 @@ pub fn len_utf8_from_first_byte(b: u8) -> usize { #[cfg(test)] mod tests { - use crate::core::flowy_str::FlowyStr; + use crate::core::flowy_str::OTString; use crate::core::interval::Interval; #[test] fn flowy_str_code_unit() { - let size = FlowyStr::from("πŸ‘‹").utf16_len(); + let size = OTString::from("πŸ‘‹").utf16_len(); assert_eq!(size, 2); - let s: FlowyStr = "πŸ‘‹ \nπŸ‘‹".into(); + let s: OTString = "πŸ‘‹ \nπŸ‘‹".into(); let output = s.sub_str(Interval::new(0, size)).unwrap(); assert_eq!(output, "πŸ‘‹"); @@ -302,7 +302,7 @@ mod tests { #[test] fn flowy_str_sub_str_in_chinese2() { - let s: FlowyStr = "😁 \n".into(); + let s: OTString = "😁 \n".into(); let size = s.utf16_len(); assert_eq!(size, 4); @@ -314,7 +314,7 @@ mod tests { #[test] fn flowy_str_sub_str_in_english() { - let s: FlowyStr = "ab".into(); + let s: OTString = "ab".into(); let size = s.utf16_len(); assert_eq!(size, 2); @@ -324,7 +324,7 @@ mod tests { #[test] fn flowy_str_utf16_code_point_iter_test2() { - let s: FlowyStr = "πŸ‘‹πŸ˜πŸ‘‹".into(); + let s: OTString = "πŸ‘‹πŸ˜πŸ‘‹".into(); let iter = s.utf16_code_point_iter(); let result = iter.skip(1).take(1).collect::(); assert_eq!(result, "😁".to_string()); diff --git a/shared-lib/lib-ot/src/core/operation/operation.rs b/shared-lib/lib-ot/src/core/operation/operation.rs index 7198c38615..482a5739ad 100644 --- a/shared-lib/lib-ot/src/core/operation/operation.rs +++ b/shared-lib/lib-ot/src/core/operation/operation.rs @@ -1,4 +1,4 @@ -use crate::core::flowy_str::FlowyStr; +use crate::core::flowy_str::OTString; use crate::core::interval::Interval; use crate::errors::OTError; use serde::{Deserialize, Serialize, __private::Formatter}; @@ -124,7 +124,7 @@ where /// Create a [Insert] operation with the given attributes pub fn insert_with_attributes(s: &str, attributes: T) -> Self { Self::Insert(Insert { - s: FlowyStr::from(s), + s: OTString::from(s), attributes, }) } @@ -132,7 +132,7 @@ where /// Create a [Insert] operation without attributes pub fn insert(s: &str) -> Self { Self::Insert(Insert { - s: FlowyStr::from(s), + s: OTString::from(s), attributes: T::default(), }) } @@ -376,7 +376,7 @@ where #[derive(Clone, Debug, Eq, PartialEq)] pub struct Insert { - pub s: FlowyStr, + pub s: OTString, pub attributes: T, } @@ -444,11 +444,11 @@ where } } -impl std::convert::From for Insert +impl std::convert::From for Insert where T: Attributes, { - fn from(s: FlowyStr) -> Self { + fn from(s: OTString) -> Self { Insert { s, attributes: T::default(), diff --git a/shared-lib/lib-ot/src/core/operation/operation_serde.rs b/shared-lib/lib-ot/src/core/operation/operation_serde.rs index d029fb0e16..40386235da 100644 --- a/shared-lib/lib-ot/src/core/operation/operation_serde.rs +++ b/shared-lib/lib-ot/src/core/operation/operation_serde.rs @@ -1,4 +1,4 @@ -use crate::core::flowy_str::FlowyStr; +use crate::core::flowy_str::OTString; use crate::core::operation::{Attributes, Insert, Operation, Retain}; use serde::{ de, @@ -249,7 +249,7 @@ where where A: SeqAccess<'de>, { - let s = match serde::de::SeqAccess::next_element::(&mut seq)? { + let s = match serde::de::SeqAccess::next_element::(&mut seq)? { Some(val) => val, None => { return Err(de::Error::invalid_length(0, &"struct Insert with 2 elements")); @@ -271,7 +271,7 @@ where where V: MapAccess<'de>, { - let mut s: Option = None; + let mut s: Option = None; let mut attributes: Option = None; while let Some(key) = map.next_key()? { match key {