-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee071db
commit 9728c71
Showing
14 changed files
with
683 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "cursor" | ||
version = "1.0.0" | ||
version = "1.1.0" | ||
authors = ["just-do-halee <[email protected]>"] | ||
homepage = "https://github.com/just-do-halee/cursor" | ||
repository = "https://github.com/just-do-halee/cursor" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#![allow(clippy::while_let_on_iterator)] | ||
|
||
use cursor::*; | ||
|
||
const SLICE: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
const STRING: &str = "this is test. 안녕하세요. 이것은 #&*@( 테스트입니다. ^^ thanks."; | ||
|
||
#[derive(Debug)] | ||
struct Counter(pub usize); | ||
|
||
impl Counter { | ||
pub fn _new() -> Self { | ||
Counter(0) | ||
} | ||
fn _clone(&self) -> Self { | ||
Counter(self.0) | ||
} | ||
fn _reset(&mut self) { | ||
self.0 = 0; | ||
} | ||
} | ||
|
||
impl Extras<u8> for Counter { | ||
fn new() -> Self { | ||
Counter::_new() | ||
} | ||
fn clone(&self) -> Self { | ||
self._clone() | ||
} | ||
fn reset(&mut self) { | ||
self._reset() | ||
} | ||
fn change(&mut self, input: &u8) { | ||
if input % 2 == 0 { | ||
self.0 += 1; | ||
} | ||
} | ||
} | ||
|
||
impl Extras<char> for Counter { | ||
fn new() -> Self { | ||
Counter::_new() | ||
} | ||
fn clone(&self) -> Self { | ||
self._clone() | ||
} | ||
fn reset(&mut self) { | ||
self._reset() | ||
} | ||
fn change(&mut self, input: &char) { | ||
if *input == ' ' { | ||
self.0 += 1; | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
example1(); | ||
example2(); | ||
example3(); | ||
example4(); | ||
println!(); | ||
} | ||
|
||
#[inline] | ||
fn example1() { | ||
// even counter | ||
let mut cursor = Cursor::new_with_extras::<Counter>(SLICE); | ||
|
||
cursor.next_to_last(); | ||
|
||
let extra = cursor.into_extras(); | ||
|
||
assert_eq!(extra.0, 5); | ||
println!("{:?}", extra); | ||
} | ||
|
||
#[inline] | ||
fn example2() { | ||
// space counter | ||
let mut cursor = StrCursor::new_with_extras::<Counter>(STRING); | ||
|
||
cursor.next_to_last(); | ||
|
||
let extra = cursor.into_extras(); | ||
|
||
assert_eq!(extra.0, 8); | ||
println!("{:?}", extra); | ||
} | ||
|
||
#[inline] | ||
fn example3() { | ||
let mut cursor = StrCursor::new(STRING); | ||
cursor += 4; | ||
assert_eq!(cursor.current(), ' '); | ||
|
||
while let Some(ch) = &mut cursor + 1 { | ||
if let Some(next) = match ch { | ||
' ' => &mut cursor + 1, | ||
'.' => &mut cursor + 2, | ||
_ => None, | ||
} { | ||
print!("{}", next); | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
fn example4() { | ||
let mut cursor = StrCursor::new(STRING); | ||
cursor.save(); | ||
assert_eq!(cursor.saved_pos(), 0); | ||
|
||
cursor += 4; | ||
assert_eq!(cursor.current(), ' '); | ||
|
||
assert_eq!(cursor.load_str(), "this "); | ||
|
||
cursor.save(); | ||
assert_eq!(cursor.saved_pos(), 4); | ||
|
||
cursor -= 2; | ||
assert_eq!(cursor.current(), 'i'); | ||
|
||
assert_eq!(cursor.load_str(), "is "); | ||
|
||
let mut cursor = StrCursor::new("한글테스트^^"); | ||
cursor.save(); | ||
assert_eq!(cursor.saved_pos(), 0); | ||
|
||
cursor += 4; | ||
assert_eq!(cursor.current(), '트'); | ||
|
||
assert_eq!(cursor.load_str(), "한글테스트"); | ||
|
||
cursor.save(); | ||
cursor -= 2; | ||
|
||
assert_eq!(cursor.load_str(), "테스트"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ fn main() { | |
example7(); | ||
example8(); | ||
example9(); | ||
println!(); | ||
} | ||
|
||
#[inline] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.