parent
bf038428c2
commit
0b76307e30
|
@ -5,20 +5,21 @@ authors = []
|
||||||
workspace = ".."
|
workspace = ".."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
xdg = "2.1.0"
|
xdg = "2.1.0" # >:c
|
||||||
serde = "1.0.71"
|
serde = "1.0.71"
|
||||||
serde_derive = "1.0.71"
|
serde_derive = "1.0.71"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
config = "0.6"
|
config = "0.6"
|
||||||
chan = "0.1.21"
|
chan = "0.1.21"
|
||||||
chan-signal = "0.3.1"
|
chan-signal = "0.3.1"
|
||||||
fnv = "1.0.3"
|
fnv = "1.0.3" # >:c
|
||||||
linkify = "0.3.1"
|
linkify = "0.3.1" # >:c
|
||||||
melib = { path = "../melib", version = "*" }
|
melib = { path = "../melib", version = "*" }
|
||||||
mime_apps = { path = "../../mime_apps", version = "*" }
|
mime_apps = { path = "../../mime_apps", version = "*" }
|
||||||
nom = "3.2.0"
|
nom = "3.2.0"
|
||||||
notify = "4.0.1"
|
notify = "4.0.1" # >:c
|
||||||
notify-rust = "^3"
|
notify-rust = "^3" # >:c
|
||||||
termion = "1.5.1"
|
termion = "1.5.1"
|
||||||
bincode = "1.0.1"
|
bincode = "1.0.1"
|
||||||
uuid = { version = "0.6", features = ["serde", "v4"] }
|
uuid = { version = "0.6", features = ["serde", "v4"] }
|
||||||
|
unicode-segmentation = "1.1.0" # >:c
|
||||||
|
|
|
@ -150,7 +150,7 @@ impl Component for ContactManager {
|
||||||
(
|
(
|
||||||
s,
|
s,
|
||||||
match v {
|
match v {
|
||||||
Field::Text(v, _, _) | Field::TextArea(v, _) => v,
|
Field::Text(v, _) => v.as_str().to_string(),
|
||||||
Field::Choice(mut v, c) => v.remove(c),
|
Field::Choice(mut v, c) => v.remove(c),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
@ -299,7 +299,7 @@ impl Pager {
|
||||||
content
|
content
|
||||||
};
|
};
|
||||||
Pager {
|
Pager {
|
||||||
text: text,
|
text,
|
||||||
cursor_pos: cursor_pos.unwrap_or(0),
|
cursor_pos: cursor_pos.unwrap_or(0),
|
||||||
height: content.size().1,
|
height: content.size().1,
|
||||||
width: content.size().0,
|
width: content.size().0,
|
||||||
|
|
|
@ -18,18 +18,16 @@ impl Default for FormFocus {
|
||||||
|
|
||||||
pub enum Field {
|
pub enum Field {
|
||||||
Text(
|
Text(
|
||||||
String,
|
UText,
|
||||||
Cursor,
|
|
||||||
Option<(Box<Fn(&Context, &str) -> Vec<String> + Send>, AutoComplete)>,
|
Option<(Box<Fn(&Context, &str) -> Vec<String> + Send>, AutoComplete)>,
|
||||||
),
|
),
|
||||||
Choice(Vec<String>, Cursor),
|
Choice(Vec<String>, Cursor),
|
||||||
TextArea(String, Cursor),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for Field {
|
impl Debug for Field {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Text(s, c, _) => fmt::Debug::fmt(s, f),
|
Text(s, _) => fmt::Debug::fmt(s, f),
|
||||||
k => fmt::Debug::fmt(k, f),
|
k => fmt::Debug::fmt(k, f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,15 +37,14 @@ use Field::*;
|
||||||
|
|
||||||
impl Default for Field {
|
impl Default for Field {
|
||||||
fn default() -> Field {
|
fn default() -> Field {
|
||||||
Field::Text(String::new(), 0, None)
|
Field::Text(UText::new(String::new()), None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Field {
|
impl Field {
|
||||||
fn as_str(&self) -> &str {
|
fn as_str(&self) -> &str {
|
||||||
match self {
|
match self {
|
||||||
Text(ref s, _, _) => s,
|
Text(ref s, _) => s.as_str(),
|
||||||
TextArea(ref s, _) => s,
|
|
||||||
Choice(ref v, cursor) => {
|
Choice(ref v, cursor) => {
|
||||||
if v.is_empty() {
|
if v.is_empty() {
|
||||||
""
|
""
|
||||||
|
@ -60,8 +57,7 @@ impl Field {
|
||||||
|
|
||||||
pub fn into_string(self) -> String {
|
pub fn into_string(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
Text(s, _, _) => s,
|
Text(s, _) => s.into_string(),
|
||||||
TextArea(s, _) => s,
|
|
||||||
Choice(mut v, cursor) => v.remove(cursor),
|
Choice(mut v, cursor) => v.remove(cursor),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,26 +71,25 @@ impl Field {
|
||||||
) {
|
) {
|
||||||
let upper_left = upper_left!(area);
|
let upper_left = upper_left!(area);
|
||||||
match self {
|
match self {
|
||||||
Text(ref term, cursor, auto_complete_fn) => {
|
Text(ref term, auto_complete_fn) => {
|
||||||
change_colors(
|
change_colors(
|
||||||
grid,
|
grid,
|
||||||
(
|
(
|
||||||
pos_inc(upper_left, (*cursor, 0)),
|
pos_inc(upper_left, (term.grapheme_pos(), 0)),
|
||||||
(pos_inc(upper_left, (*cursor, 0))),
|
(pos_inc(upper_left, (term.grapheme_pos(), 0))),
|
||||||
),
|
),
|
||||||
Color::Default,
|
Color::Default,
|
||||||
Color::Byte(248),
|
Color::Byte(248),
|
||||||
);
|
);
|
||||||
if term.chars().count() <= 2 {
|
if term.grapheme_len() <= 2 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let Some((auto_complete_fn, auto_complete)) = auto_complete_fn {
|
if let Some((auto_complete_fn, auto_complete)) = auto_complete_fn {
|
||||||
let entries = auto_complete_fn(context, term);
|
let entries = auto_complete_fn(context, term.as_str());
|
||||||
auto_complete.set_suggestions(entries);
|
auto_complete.set_suggestions(entries);
|
||||||
auto_complete.draw(grid, secondary_area, context);
|
auto_complete.draw(grid, secondary_area, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TextArea(_, _) => {}
|
|
||||||
Choice(_, _cursor) => {}
|
Choice(_, _cursor) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,39 +107,35 @@ impl Component for Field {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
|
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
|
||||||
if let Text(ref mut s, ref mut cursor, Some((_, auto_complete))) = self {
|
if let Text(ref mut s, Some((_, auto_complete))) = self {
|
||||||
match event.event_type {
|
if let UIEventType::InsertInput(Key::Char('\t')) = event.event_type {
|
||||||
UIEventType::InsertInput(Key::Char('\t')) => {
|
if let Some(suggestion) = auto_complete.get_suggestion() {
|
||||||
if let Some(suggestion) = auto_complete.get_suggestion() {
|
*s = UText::new(suggestion);
|
||||||
*s = suggestion;
|
let len = s.as_str().len().saturating_sub(1);
|
||||||
*cursor = s.chars().count();
|
s.set_cursor(len);
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match event.event_type {
|
match event.event_type {
|
||||||
UIEventType::InsertInput(Key::Up) => {
|
UIEventType::InsertInput(Key::Up) => {
|
||||||
if let Text(_, _, Some((_, auto_complete))) = self {
|
if let Text(_, Some((_, auto_complete))) = self {
|
||||||
auto_complete.dec_cursor();
|
auto_complete.dec_cursor();
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UIEventType::InsertInput(Key::Down) => {
|
UIEventType::InsertInput(Key::Down) => {
|
||||||
if let Text(_, _, Some((_, auto_complete))) = self {
|
if let Text(_, Some((_, auto_complete))) = self {
|
||||||
auto_complete.inc_cursor();
|
auto_complete.inc_cursor();
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UIEventType::InsertInput(Key::Right) => match self {
|
UIEventType::InsertInput(Key::Right) => match self {
|
||||||
TextArea(ref s, ref mut cursor) | Text(ref s, ref mut cursor, _) => {
|
Text(ref mut s, _) => {
|
||||||
if *cursor < s.len() {
|
s.cursor_inc();
|
||||||
*cursor += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Choice(ref vec, ref mut cursor) => {
|
Choice(ref vec, ref mut cursor) => {
|
||||||
*cursor = if *cursor == vec.len().saturating_sub(1) {
|
*cursor = if *cursor == vec.len().saturating_sub(1) {
|
||||||
|
@ -155,12 +146,8 @@ impl Component for Field {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UIEventType::InsertInput(Key::Left) => match self {
|
UIEventType::InsertInput(Key::Left) => match self {
|
||||||
TextArea(_, ref mut cursor) | Text(_, ref mut cursor, _) => {
|
Text(ref mut s, _) => {
|
||||||
if *cursor == 0 {
|
s.cursor_dec();
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
*cursor -= 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Choice(_, ref mut cursor) => {
|
Choice(_, ref mut cursor) => {
|
||||||
if *cursor == 0 {
|
if *cursor == 0 {
|
||||||
|
@ -170,27 +157,14 @@ impl Component for Field {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
UIEventType::InsertInput(Key::Char(k)) => match self {
|
UIEventType::InsertInput(Key::Char(k)) => if let Text(ref mut s, _) = self {
|
||||||
Text(ref mut s, ref mut cursor, _) | TextArea(ref mut s, ref mut cursor) => {
|
s.insert_char(k);
|
||||||
s.insert(*cursor, k);
|
|
||||||
*cursor += 1;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
},
|
},
|
||||||
UIEventType::InsertInput(Key::Backspace) => match self {
|
UIEventType::InsertInput(Key::Backspace) => match self {
|
||||||
Text(ref mut s, ref mut cursor, ref mut auto_complete) => {
|
Text(ref mut s, auto_complete) => {
|
||||||
if *cursor > 0 {
|
s.backspace();
|
||||||
*cursor -= 1;
|
if let Some(ac) = auto_complete.as_mut() {
|
||||||
s.remove(*cursor);
|
ac.1.set_suggestions(Vec::new());
|
||||||
}
|
|
||||||
auto_complete
|
|
||||||
.as_mut()
|
|
||||||
.map(|ac| ac.1.set_suggestions(Vec::new()));
|
|
||||||
}
|
|
||||||
TextArea(ref mut s, ref mut cursor) => {
|
|
||||||
if *cursor > 0 {
|
|
||||||
*cursor -= 1;
|
|
||||||
s.remove(*cursor);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
@ -268,11 +242,6 @@ impl FormWidget {
|
||||||
self.layout.push(value.0.clone());
|
self.layout.push(value.0.clone());
|
||||||
self.fields.insert(value.0, Choice(value.1, 0));
|
self.fields.insert(value.0, Choice(value.1, 0));
|
||||||
}
|
}
|
||||||
pub fn push_text_area(&mut self, value: (String, String)) {
|
|
||||||
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
|
|
||||||
self.layout.push(value.0.clone());
|
|
||||||
self.fields.insert(value.0, TextArea(value.1, 0));
|
|
||||||
}
|
|
||||||
pub fn push_cl(
|
pub fn push_cl(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: (
|
value: (
|
||||||
|
@ -285,13 +254,16 @@ impl FormWidget {
|
||||||
self.layout.push(value.0.clone());
|
self.layout.push(value.0.clone());
|
||||||
self.fields.insert(
|
self.fields.insert(
|
||||||
value.0,
|
value.0,
|
||||||
Text(value.1, 0, Some((value.2, AutoComplete::new(Vec::new())))),
|
Text(
|
||||||
|
UText::new(value.1),
|
||||||
|
Some((value.2, AutoComplete::new(Vec::new()))),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
pub fn push(&mut self, value: (String, String)) {
|
pub fn push(&mut self, value: (String, String)) {
|
||||||
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
|
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
|
||||||
self.layout.push(value.0.clone());
|
self.layout.push(value.0.clone());
|
||||||
self.fields.insert(value.0, Text(value.1, 0, None));
|
self.fields.insert(value.0, Text(UText::new(value.1), None));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, index: usize, value: (String, Field)) {
|
pub fn insert(&mut self, index: usize, value: (String, Field)) {
|
||||||
|
@ -614,7 +586,7 @@ impl Component for AutoComplete {
|
||||||
);
|
);
|
||||||
context.dirty_areas.push_back(area);
|
context.dirty_areas.push_back(area);
|
||||||
}
|
}
|
||||||
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
|
fn process_event(&mut self, _event: &mut UIEvent, _context: &mut Context) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
fn is_dirty(&self) -> bool {
|
fn is_dirty(&self) -> bool {
|
||||||
|
@ -649,7 +621,7 @@ impl AutoComplete {
|
||||||
);
|
);
|
||||||
let width = content.cols();
|
let width = content.cols();
|
||||||
for (i, e) in entries.iter().enumerate() {
|
for (i, e) in entries.iter().enumerate() {
|
||||||
let (x, _) = write_string_to_grid(
|
write_string_to_grid(
|
||||||
e,
|
e,
|
||||||
&mut content,
|
&mut content,
|
||||||
Color::Byte(23),
|
Color::Byte(23),
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
use self::serde::de::Visitor;
|
use self::serde::de::Visitor;
|
||||||
use self::serde::{de, Deserialize, Deserializer};
|
use self::serde::{de, Deserialize, Deserializer};
|
||||||
|
extern crate unicode_segmentation;
|
||||||
|
use self::unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod position;
|
mod position;
|
||||||
|
@ -28,8 +30,12 @@ mod position;
|
||||||
mod cells;
|
mod cells;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod keys;
|
mod keys;
|
||||||
|
mod grapheme_clusters;
|
||||||
|
mod text_editing;
|
||||||
mod wcwidth;
|
mod wcwidth;
|
||||||
pub use self::cells::*;
|
pub use self::cells::*;
|
||||||
|
pub use self::grapheme_clusters::*;
|
||||||
pub use self::keys::*;
|
pub use self::keys::*;
|
||||||
pub use self::position::*;
|
pub use self::position::*;
|
||||||
|
pub use self::text_editing::*;
|
||||||
pub use self::wcwidth::*;
|
pub use self::wcwidth::*;
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,131 @@
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct UText {
|
||||||
|
content: String,
|
||||||
|
cursor_pos: usize,
|
||||||
|
grapheme_cursor_pos: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UText {
|
||||||
|
pub fn new(content: String) -> Self {
|
||||||
|
UText {
|
||||||
|
content,
|
||||||
|
cursor_pos: 0,
|
||||||
|
grapheme_cursor_pos: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_cursor(&mut self, cursor_pos: usize) {
|
||||||
|
if cursor_pos >= self.content.len() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (first, _) = self.content.split_at(cursor_pos);
|
||||||
|
self.grapheme_cursor_pos = split_graphemes(first).len();
|
||||||
|
self.cursor_pos = cursor_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_str(&self) -> &str {
|
||||||
|
self.content.as_str()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_string(self) -> String {
|
||||||
|
self.content
|
||||||
|
}
|
||||||
|
pub fn grapheme_len(&self) -> usize {
|
||||||
|
split_graphemes(&self.content).len()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cursor_inc(&mut self) {
|
||||||
|
if self.cursor_pos >= self.content.len() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let (_, right) = std::dbg!(self.content.split_at(self.cursor_pos));
|
||||||
|
if let Some((_, graph)) = std::dbg!(next_grapheme(right)) {
|
||||||
|
self.cursor_pos += graph.len();
|
||||||
|
self.grapheme_cursor_pos += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn cursor_dec(&mut self) {
|
||||||
|
if self.cursor_pos == 0 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let (left, _) = std::dbg!(self.content.split_at(self.cursor_pos));
|
||||||
|
if let Some((_, graph)) = std::dbg!(last_grapheme(left)) {
|
||||||
|
self.cursor_pos -= graph.len();
|
||||||
|
self.grapheme_cursor_pos -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cursor_pos(&self) -> usize {
|
||||||
|
self.cursor_pos
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn grapheme_pos(&self) -> usize {
|
||||||
|
self.grapheme_cursor_pos
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Insert code point `k` in position `self.cursor_pos`:
|
||||||
|
*
|
||||||
|
* before:
|
||||||
|
*
|
||||||
|
* self.content = xxxxxx....xxxxxxx;
|
||||||
|
* ^
|
||||||
|
* self.cursor_pos
|
||||||
|
*
|
||||||
|
* after:
|
||||||
|
*
|
||||||
|
* self.content = xxxxxx....xxxxkxxx;
|
||||||
|
* ^
|
||||||
|
* self.cursor_pos
|
||||||
|
*/
|
||||||
|
pub fn insert_char(&mut self, k: char) {
|
||||||
|
self.content.insert(self.cursor_pos, k);
|
||||||
|
self.cursor_pos += k.len_utf8();
|
||||||
|
self.grapheme_cursor_pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* remove grapheme cluster that ends on `self.cursor_pos`:
|
||||||
|
*
|
||||||
|
* before:
|
||||||
|
*
|
||||||
|
* self.content = xxxxxx....xxggxxx;
|
||||||
|
* ^
|
||||||
|
* self.cursor_pos
|
||||||
|
*
|
||||||
|
* after:
|
||||||
|
*
|
||||||
|
* self.content = xxxxxx....xxxxxx;
|
||||||
|
* ^
|
||||||
|
* self.cursor_pos
|
||||||
|
*/
|
||||||
|
pub fn backspace(&mut self) {
|
||||||
|
if self.content.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let (offset, graph_len) = {
|
||||||
|
/*
|
||||||
|
* Split string at cursor_pos:
|
||||||
|
*/
|
||||||
|
let (left, _) = self.content.split_at(self.cursor_pos);
|
||||||
|
/*
|
||||||
|
* left = xxxxxx....xxgg;
|
||||||
|
* right = xxx;
|
||||||
|
*/
|
||||||
|
if let Some((offset, graph)) = std::dbg!(last_grapheme(left)) {
|
||||||
|
(offset, graph.len())
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.cursor_dec();
|
||||||
|
|
||||||
|
self.content
|
||||||
|
.drain(std::dbg!(offset..offset + graph_len))
|
||||||
|
.count();
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,9 @@
|
||||||
* Markus Kuhn -- 2001-09-08 -- public domain
|
* Markus Kuhn -- 2001-09-08 -- public domain
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// TODO: Spacing widths
|
||||||
|
// Update to Unicode 12
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! big_if_true {
|
macro_rules! big_if_true {
|
||||||
($a:expr) => {
|
($a:expr) => {
|
||||||
|
@ -20,6 +23,76 @@ macro_rules! big_if_true {
|
||||||
type WChar = u32;
|
type WChar = u32;
|
||||||
type Interval = (WChar, WChar);
|
type Interval = (WChar, WChar);
|
||||||
|
|
||||||
|
pub struct CodePointsIterator<'a> {
|
||||||
|
rest: &'a [u8],
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* UTF-8 uses a system of binary prefixes, in which the high bits of each byte mark whether it’s a single byte, the beginning of a multi-byte sequence, or a continuation byte; the remaining bits, concatenated, give the code point index. This table shows how it works:
|
||||||
|
*
|
||||||
|
* UTF-8 (binary) Code point (binary) Range
|
||||||
|
* 0xxxxxxx xxxxxxx U+0000–U+007F
|
||||||
|
* 110xxxxx 10yyyyyy xxxxxyyyyyy U+0080–U+07FF
|
||||||
|
* 1110xxxx 10yyyyyy 10zzzzzz xxxxyyyyyyzzzzzz U+0800–U+FFFF
|
||||||
|
* 11110xxx 10yyyyyy 10zzzzzz 10wwwwww xxxyyyyyyzzzzzzwwwwww U+10000–U+10FFFF
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
impl<'a> Iterator for CodePointsIterator<'a> {
|
||||||
|
type Item = WChar;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<WChar> {
|
||||||
|
println!("rest = {:?}", self.rest);
|
||||||
|
if self.rest.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
/* Input is UTF-8 valid strings, guaranteed by Rust's std */
|
||||||
|
if self.rest[0] & 0b1000_0000 == 0x0 {
|
||||||
|
let ret: WChar = self.rest[0] as WChar;
|
||||||
|
self.rest = &self.rest[1..];
|
||||||
|
return Some(ret);
|
||||||
|
}
|
||||||
|
if self.rest[0] & 0b1110_0000 == 0b1100_0000 {
|
||||||
|
let ret: WChar = (self.rest[0] as WChar & 0b0001_1111).rotate_left(6)
|
||||||
|
+ (self.rest[1] as WChar & 0b0111_1111);
|
||||||
|
self.rest = &self.rest[2..];
|
||||||
|
return Some(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.rest[0] & 0b1111_0000 == 0b1110_0000 {
|
||||||
|
let ret: WChar = (self.rest[0] as WChar & 0b0000_0111).rotate_left(12)
|
||||||
|
+ (self.rest[1] as WChar & 0b0011_1111).rotate_left(6)
|
||||||
|
+ (self.rest[2] as WChar & 0b0011_1111);
|
||||||
|
self.rest = &self.rest[3..];
|
||||||
|
return Some(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
let ret: WChar = (self.rest[0] as WChar & 0b0000_0111).rotate_left(18)
|
||||||
|
+ (self.rest[1] as WChar & 0b0011_1111).rotate_left(12)
|
||||||
|
+ (self.rest[2] as WChar & 0b0011_1111).rotate_left(6)
|
||||||
|
+ (self.rest[3] as WChar & 0b0011_1111);
|
||||||
|
self.rest = &self.rest[4..];
|
||||||
|
Some(ret)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub trait CodePointsIter {
|
||||||
|
fn code_points(&self) -> CodePointsIterator;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CodePointsIter for str {
|
||||||
|
fn code_points(&self) -> CodePointsIterator {
|
||||||
|
CodePointsIterator {
|
||||||
|
rest: self.as_bytes(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl CodePointsIter for &str {
|
||||||
|
fn code_points(&self) -> CodePointsIterator {
|
||||||
|
CodePointsIterator {
|
||||||
|
rest: self.as_bytes(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* auxiliary function for binary search in Interval table */
|
/* auxiliary function for binary search in Interval table */
|
||||||
fn bisearch(ucs: WChar, table: &'static [Interval]) -> bool {
|
fn bisearch(ucs: WChar, table: &'static [Interval]) -> bool {
|
||||||
let mut min = 0;
|
let mut min = 0;
|
||||||
|
@ -74,7 +147,7 @@ fn bisearch(ucs: WChar, table: &'static [Interval]) -> bool {
|
||||||
* in ISO 10646.
|
* in ISO 10646.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fn wcwidth(ucs: WChar) -> Option<usize> {
|
pub fn wcwidth(ucs: WChar) -> Option<usize> {
|
||||||
/* sorted list of non-overlapping intervals of non-spacing characters */
|
/* sorted list of non-overlapping intervals of non-spacing characters */
|
||||||
let combining: &'static [Interval] = &[
|
let combining: &'static [Interval] = &[
|
||||||
(0x0300, 0x034E),
|
(0x0300, 0x034E),
|
||||||
|
@ -235,7 +308,7 @@ fn wcswidth(mut pwcs: WChar, mut n: usize) -> Option<usize> {
|
||||||
* encodings who want to migrate to UCS. It is not otherwise
|
* encodings who want to migrate to UCS. It is not otherwise
|
||||||
* recommended for general use.
|
* recommended for general use.
|
||||||
*/
|
*/
|
||||||
fn wcwidth_cjk(ucs: WChar) -> Option<usize> {
|
pub fn wcwidth_cjk(ucs: WChar) -> Option<usize> {
|
||||||
/* sorted list of non-overlapping intervals of East Asian Ambiguous
|
/* sorted list of non-overlapping intervals of East Asian Ambiguous
|
||||||
* characters */
|
* characters */
|
||||||
let ambiguous: &'static [Interval] = &[
|
let ambiguous: &'static [Interval] = &[
|
||||||
|
|
Loading…
Reference in New Issue