Skip to content

Commit d354bb2

Browse files
authored
Add interpretation function from byte to character (#154)
1 parent 73e45be commit d354bb2

8 files changed

Lines changed: 81 additions & 8 deletions

File tree

bin/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.3.9-git
4+
5+
### Patch
6+
7+
- Update `data-encoding` version
8+
39
## 0.3.8
410

511
### Patch

bin/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "data-encoding-bin"
3-
version = "0.3.8"
3+
version = "0.3.9-git"
44
authors = ["Julien Cretin <git@ia0.eu>"]
55
license = "MIT"
66
edition = "2021"
@@ -17,5 +17,5 @@ name = "data-encoding"
1717
path = "src/main.rs"
1818

1919
[dependencies]
20-
data-encoding = { version = "2.10.0", path = "../lib" }
20+
data-encoding = { version = "2.11.0-git", path = "../lib" }
2121
getopts = "0.2"

cmp/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() {
66
.compiler(compiler)
77
.define("COMPILER", Some(*compiler))
88
.file("src/ref.c")
9+
.flag_if_supported("-Wno-unterminated-string-initialization")
910
.compile(&format!("libref_{}.a", compiler));
1011
}
1112
}

lib/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 2.11.0-git
4+
5+
### Minor
6+
7+
- Add `Character` and `Encoding::interpret_byte()` to help with unsupported custom processing
8+
39
## 2.10.0
410

511
### Minor

lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "data-encoding"
3-
version = "2.10.0"
3+
version = "2.11.0-git"
44
authors = ["Julien Cretin <git@ia0.eu>"]
55
license = "MIT"
66
edition = "2018"

lib/macro/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "data-encoding-macro"
3-
version = "0.1.19"
3+
version = "0.1.20-git"
44
authors = ["Julien Cretin <cretin@google.com>"]
55
license = "MIT"
66
edition = "2018"
@@ -14,5 +14,5 @@ description = "Macros for data-encoding"
1414
include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"]
1515

1616
[dependencies]
17-
data-encoding = { version = "2.10.0", path = "..", default-features = false }
18-
data-encoding-macro-internal = { version = "0.1.17", path = "internal" }
17+
data-encoding = { version = "2.11.0-git", path = "..", default-features = false }
18+
data-encoding-macro-internal = { version = "0.1.18-git", path = "internal" }

lib/macro/internal/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "data-encoding-macro-internal"
3-
version = "0.1.17"
3+
version = "0.1.18-git"
44
authors = ["Julien Cretin <cretin@google.com>"]
55
license = "MIT"
66
edition = "2018"
@@ -14,7 +14,7 @@ include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"]
1414
proc-macro = true
1515

1616
[dependencies.data-encoding]
17-
version = "2.10.0"
17+
version = "2.11.0-git"
1818
path = "../.."
1919
default-features = false
2020
features = ["alloc"]

lib/src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,56 @@ pub enum BitOrder {
840840
#[cfg(feature = "alloc")]
841841
use crate::BitOrder::*;
842842

843+
/// Interpretation of a byte for decoding purposes
844+
///
845+
/// For a given encoding, a byte can either be a symbol of that encoding (with a value within the
846+
/// number of symbols of that encoding), a padding character, an ignored character, or an invalid
847+
/// character.
848+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
849+
pub enum Character {
850+
/// A symbol
851+
Symbol {
852+
/// The value of the symbol
853+
value: usize,
854+
},
855+
856+
/// A padding character
857+
Padding,
858+
859+
/// An ignored character
860+
Ignored,
861+
862+
/// An invalid character
863+
Invalid,
864+
}
865+
866+
impl Character {
867+
/// Returns whether the character is a symbol
868+
///
869+
/// If the character is a symbol, its value is returned.
870+
pub fn is_symbol(self) -> Option<usize> {
871+
match self {
872+
Character::Symbol { value } => Some(value),
873+
_ => None,
874+
}
875+
}
876+
877+
/// Returns whether the character is padding
878+
pub fn is_padding(self) -> bool {
879+
matches!(self, Character::Padding)
880+
}
881+
882+
/// Returns whether the character is ignored
883+
pub fn is_ignored(self) -> bool {
884+
matches!(self, Character::Ignored)
885+
}
886+
887+
/// Returns whether the character is invalid
888+
pub fn is_invalid(self) -> bool {
889+
matches!(self, Character::Invalid)
890+
}
891+
}
892+
843893
#[doc(hidden)]
844894
#[cfg(feature = "alloc")]
845895
pub type InternalEncoding = Cow<'static, [u8]>;
@@ -1600,6 +1650,16 @@ impl Encoding {
16001650
self.bit()
16011651
}
16021652

1653+
/// Interprets a byte as a character
1654+
pub fn interpret_byte(&self, byte: u8) -> Character {
1655+
match self.val()[byte as usize] {
1656+
INVALID => Character::Invalid,
1657+
IGNORE => Character::Ignored,
1658+
PADDING => Character::Padding,
1659+
value => Character::Symbol { value: value as usize },
1660+
}
1661+
}
1662+
16031663
/// Returns whether the encoding is canonical
16041664
///
16051665
/// An encoding is not canonical if one of the following conditions holds:

0 commit comments

Comments
 (0)