-
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.
Rename locale to format to avoid confusion and update documentation.
- Loading branch information
1 parent
f2e28da
commit acd98d0
Showing
23 changed files
with
377 additions
and
331 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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
//! [Format] trait for input format and built-in formats. | ||
use crate::{ | ||
stream::{COMMA, CR, HT, LF, SP}, | ||
utf8char::FixedUtf8Char, | ||
}; | ||
|
||
/// Trait for input format. | ||
pub trait Format<Char = char> { | ||
/// Get the list of whitespace characters. | ||
fn skipped_chars(&self) -> &[Char]; | ||
} | ||
|
||
impl<L: Format<Char> + ?Sized, Char> Format<Char> for &L { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[Char] { | ||
<L as Format<Char>>::skipped_chars(self) | ||
} | ||
} | ||
|
||
/// Default Format. | ||
/// | ||
/// Whitespace characters here are `' '`, `'\t'`, `'\n'`, and `'\r'`. | ||
pub struct Default; | ||
|
||
const WHITE_SPACES: [FixedUtf8Char; 4] = [SP, HT, LF, CR]; | ||
|
||
impl Format<FixedUtf8Char> for Default { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[FixedUtf8Char] { | ||
&WHITE_SPACES | ||
} | ||
} | ||
|
||
impl Format<char> for Default { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[char] { | ||
&[' ', '\t', '\n', '\r'] | ||
} | ||
} | ||
|
||
/// Format for CSV. | ||
/// | ||
/// Whitespace characters here are `' '`, `'\t'`, `','`, `'\n'`, and `'\r'`. | ||
pub struct CSV; | ||
|
||
const CSV_SEP: [FixedUtf8Char; 5] = [SP, HT, COMMA, LF, CR]; | ||
|
||
impl Format<FixedUtf8Char> for CSV { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[FixedUtf8Char] { | ||
&CSV_SEP | ||
} | ||
} | ||
|
||
impl Format<char> for CSV { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[char] { | ||
&[' ', '\t', ',', '\n', '\r'] | ||
} | ||
} | ||
|
||
/// Special format that skip the given characters. | ||
pub struct Skip<Char = char>(Vec<Char>); | ||
|
||
impl<Char: From<C> + Ord, C> FromIterator<C> for Skip<Char> { | ||
#[inline] | ||
fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self { | ||
let mut v: Vec<_> = iter.into_iter().map(From::from).collect(); | ||
v.sort(); | ||
v.dedup(); | ||
Self(v) | ||
} | ||
} | ||
|
||
impl Format<FixedUtf8Char> for Skip<FixedUtf8Char> { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[FixedUtf8Char] { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl Format<char> for Skip<char> { | ||
#[inline] | ||
fn skipped_chars(&self) -> &[char] { | ||
&self.0 | ||
} | ||
} | ||
|
||
/// Create a [Format] instance that skip the given characters. | ||
#[inline] | ||
pub fn skip<Char: Ord, T: IntoIterator<Item = Char>>(iter: T) -> Skip<Char> { | ||
iter.into_iter().collect() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Default; | ||
use crate::{ | ||
fmt::{Format, Skip, CSV, WHITE_SPACES}, | ||
utf8char::FixedUtf8Char, | ||
}; | ||
|
||
#[test] | ||
fn equivalence() { | ||
assert_eq!( | ||
<Default as Format<char>>::skipped_chars(&Default), | ||
<Default as Format<FixedUtf8Char>>::skipped_chars(&Default), | ||
); | ||
assert_eq!( | ||
<&Default as Format<char>>::skipped_chars(&&Default), | ||
<&Default as Format<FixedUtf8Char>>::skipped_chars(&&Default), | ||
); | ||
assert_eq!( | ||
<CSV as Format<char>>::skipped_chars(&CSV), | ||
<CSV as Format<FixedUtf8Char>>::skipped_chars(&CSV), | ||
); | ||
assert_eq!( | ||
<&CSV as Format<char>>::skipped_chars(&&CSV), | ||
<&CSV as Format<FixedUtf8Char>>::skipped_chars(&&CSV), | ||
); | ||
|
||
let seps = [' ', '\t', '\n', '\r']; | ||
assert_eq!( | ||
<Skip<char> as Format<char>>::skipped_chars(&FromIterator::from_iter(seps)), | ||
<Skip<FixedUtf8Char> as Format<FixedUtf8Char>>::skipped_chars( | ||
&FromIterator::from_iter(seps) | ||
), | ||
); | ||
assert_eq!( | ||
<Skip<char> as Format<char>>::skipped_chars(&FromIterator::from_iter(WHITE_SPACES)), | ||
<Skip<FixedUtf8Char> as Format<FixedUtf8Char>>::skipped_chars( | ||
&FromIterator::from_iter(WHITE_SPACES) | ||
), | ||
); | ||
} | ||
} |
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
Oops, something went wrong.