Skip to content

Commit

Permalink
Config for string encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Jun 19, 2024
1 parent 67fdd36 commit 76ffd71
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 53 deletions.
12 changes: 6 additions & 6 deletions projects/nyar-string/src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ pub enum TextEncoding {
}

pub(crate) struct TextEncodingDisplay<'i> {
text: &'i [u8],
encoding: TextEncoding,
pub text: &'i [u8],
pub encoding: TextEncoding,
}

impl<'i> Debug for TextEncodingDisplay<'i> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.encoding {
TextEncoding::Unknown => {
for byte in self.text {
write!(f, "{}", byte)
write!(f, "{}", byte)?
}
}
TextEncoding::UTF8 => f.write_str(&String::from_utf8_lossy(self.text)),
TextEncoding::UTF16LE => f.write_str(&String::from_utf16le_lossy(self.text)),
TextEncoding::UTF16BE => f.write_str(&String::from_utf16be_lossy(self.text)),
TextEncoding::UTF8 => f.write_str(&String::from_utf8_lossy(self.text))?,
TextEncoding::UTF16LE => f.write_str(&String::from_utf16le_lossy(self.text))?,
TextEncoding::UTF16BE => f.write_str(&String::from_utf16be_lossy(self.text))?,
TextEncoding::UTF32 => {
unimplemented!()
}
Expand Down
6 changes: 0 additions & 6 deletions projects/nyar-string/src/errors.rs

This file was deleted.

20 changes: 2 additions & 18 deletions projects/nyar-string/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
#![feature(str_from_utf16_endian)]

// #![feature(once_cell)]
//
// pub use errors::{Error, Result};
//
// pub use crate::{
// manager::{StringID, StringManager, STRING_MANAGER},
// smart::{
// inlined::{InlineBuffer, LENGTH_MASK, MAX_SIZE},
// SmartString, SmartStringKind,
// },
// };
//
// mod manager;
// mod smart;
//
pub(crate) mod encoding;
pub(crate) mod errors;
pub(crate) mod utf8;
pub(crate) mod text;
pub mod view;

pub use crate::{encoding::TextEncoding, utf8::text::AnyText};
pub use crate::{encoding::TextEncoding, text::AnyText};
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use super::*;
use crate::{encoding::TextEncodingDisplay, TextEncoding};
use shredder::{
marker::{GcDrop, GcSafe},
Gc, Scan, Scanner,
};
use std::fmt::{Debug, Display, Formatter};

/// UTF8 encoded exclusive, mutable string
Expand Down Expand Up @@ -28,16 +32,10 @@ impl Default for AnyText {

impl Debug for AnyText {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let w = &mut f.debug_struct("Text").field("encode", &self.encoding);
match self.encoding {
TextEncoding::Unknown => w.field("data", self.data),
TextEncoding::UTF8 => {}
TextEncoding::UTF16LE => {}
TextEncoding::UTF32BE => {}
TextEncoding::UTF32 => {}
}

f.debug_struct("Text").field("encode", &"UTF8").finish()
f.debug_struct("Text")
.field("encode", &self.encoding)
.field("data", &TextEncodingDisplay { text: &self.data.get(), encoding: self.encoding })
.finish()
}
}

Expand All @@ -50,32 +48,28 @@ impl Clone for AnyText {
}
else {
// 共享不可变
Self { mutable: false, data: Default::default() }
Self { mutable: false, encoding: self.encoding, data: Default::default() }
}
}
}

impl Display for AnyText {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.encoding {
TextEncoding::Unknown => {}
TextEncoding::UTF8 => {}
TextEncoding::UTF16LE => {}
TextEncoding::UTF32BE => {}
TextEncoding::UTF32 => {}
}
TextEncodingDisplay { text: &self.data.get(), encoding: self.encoding }.fmt(f)
}
}

impl AnyText {
pub fn new<S: Into<String>>(text: S, mutable: bool) -> Self {
Self { mutable, encoding: TextEncoding::Unknown, data: Gc::new(vec![]) }
Self { mutable, encoding: TextEncoding::UTF8, data: Gc::new(text.into().into_bytes()) }
}
pub fn with_encoding(self, encoding: TextEncoding) -> Self {
Self { encoding, ..self }
}
pub fn with_encoding(&self, encoding: TextEncoding) {}
}

impl From<String> for AnyText {
fn from(value: String) -> Self {
Self { mutable: false, data: Gc::new(vec![]) }
Self { mutable: false, encoding: TextEncoding::UTF8, data: Gc::new(value.into_bytes()) }
}
}
9 changes: 8 additions & 1 deletion projects/nyar-string/src/view/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use crate::utf8::*;
use crate::text::*;
use std::ops::Range;

/// UTF8 encoded shared, immutable string
#[derive(Clone, Debug)]
pub struct UTF8View {
raw: AnyText,
span: Range<usize>,
}

impl AnyText {
pub fn view(&self, span: Range<usize>) -> UTF8View {
UTF8View { raw: self.clone(), span }
}
}

0 comments on commit 76ffd71

Please sign in to comment.