Skip to content

Commit

Permalink
improve cjk in displaying; tag 0.6.11
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jun 26, 2024
1 parent 4b9f2a3 commit a9f0d29
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cirru_edn"
version = "0.6.10"
version = "0.6.11"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2021"
license = "MIT"
Expand All @@ -18,6 +18,7 @@ cirru_parser = "0.1.30"
hex = "0.4.3"
lazy_static = "1.4.0"
bincode = "2.0.0-rc.3"
cjk = "0.2.5"

[dev-dependencies]
criterion = "0.5.1"
Expand Down
21 changes: 17 additions & 4 deletions src/edn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
},
collections::{HashMap, HashSet},
convert::{TryFrom, TryInto},
fmt,
fmt::{self, Write},
hash::{Hash, Hasher},
iter::FromIterator,
ptr,
Expand Down Expand Up @@ -63,7 +63,15 @@ impl fmt::Display for Edn {
if is_simple_token(s) {
f.write_fmt(format_args!("|{}", s))
} else {
f.write_fmt(format_args!("\"|{}\"", s))
f.write_str("\"|")?;
for c in s.chars() {
if is_simple_char(c) {
f.write_char(c)?;
} else {
f.write_str(&c.escape_default().to_string())?;
}
}
f.write_char('"')
}
}
Self::Quote(v) => f.write_fmt(format_args!("(quote {})", v)),
Expand Down Expand Up @@ -122,9 +130,14 @@ impl fmt::Display for Edn {
}
}

/// check if a char is simple enough to be printed without quotes
pub fn is_simple_char(c: char) -> bool {
matches!(c, '0'..='9' | 'A'..='Z' | 'a'..='z' | '-' | '?' | '.' | '$' | ',') || cjk::is_cjk_codepoint(c)
}

fn is_simple_token(tok: &str) -> bool {
for s in tok.bytes() {
if !matches!(s, b'0'..=b'9' | b'A'..=b'Z'| b'a'..=b'z'| b'-' | b'?' | b'.'| b'$' | b',') {
for s in tok.chars() {
if !is_simple_char(s) {
return false;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use std::vec;

use cirru_parser::{Cirru, CirruWriterOptions};

pub use edn::{DynEq, Edn, EdnAnyRef, EdnListView, EdnMapView, EdnRecordView, EdnSetView, EdnTupleView};
pub use edn::{
is_simple_char, DynEq, Edn, EdnAnyRef, EdnListView, EdnMapView, EdnRecordView, EdnSetView, EdnTupleView,
};
pub use tag::EdnTag;

/// parse Cirru code into data
Expand Down
9 changes: 8 additions & 1 deletion tests/display_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate cirru_edn;

use std::{sync::Arc, vec};

use cirru_edn::{Edn, EdnRecordView, EdnTag};
use cirru_edn::{Edn, EdnListView, EdnRecordView, EdnTag};

#[test]
fn display_data() {
Expand All @@ -22,3 +22,10 @@ fn display_data() {
let t2 = Edn::from((Arc::new(Edn::tag("t")), vec![Edn::Number(1.0), Edn::Number(2.0)]));
assert_eq!(format!("{t2}"), "(:: :t 1 2)");
}

#[test]
fn display_with_cjk() {
let r = Edn::List(EdnListView(vec![Edn::str("你好"), Edn::str("世界"), Edn::str("海 洋")]));

assert_eq!(format!("{r}"), "([] |你好 |世界 \"|海 洋\")");
}

0 comments on commit a9f0d29

Please sign in to comment.