Skip to content

Repository files navigation

Yore

A Rust library for decoding and encoding character sets based on OEM code pages.

yore at crates.ioyore at docs.rs

Features

  • Fast performance*
  • Minimal memory usage with Cow and shrink_to_fit
  • Easy-to-use API
  • Broad range of supported code pages
  • Handles code pages with redefined ASCII characters (<0x80), such as '٪' in CP864
  • no_std support, with or without an allocator — down to allocation-free encode_char / decode_byte primitives for embedded use

Usage

Add yore to your Cargo.toml file.

[dependencies]
yore = "2.1.1"

Examples

Using a specific code page

use yore::code_pages::{CP857,CP850};use yore::{DecodeError,EncodeError};// Vec contains ASCII "text"let bytes = vec![116,101,120,116];// Vec contains ASCII "text " and codepoint 231let bytes_undefined = vec![116,101,120,116,32,231];// Notice that decoding CP850 can't fail because it is completely definedassert_eq!(CP850.decode(&bytes),"text");// However, CP857 can failassert_eq!(CP857.decode(&bytes).unwrap(),"text");// "text " + codepoint 231 assert!(matches!(CP857.decode(&bytes_undefined),DecodeError));// Lossy decoding won't fail due to fallbackassert_eq!(CP857.decode_lossy(&bytes_undefined),"text �");// Encodingassert_eq!(CP850.encode("text").unwrap(), bytes);assert!(matches!(CP850.encode("text 🦀"),EncodeError));assert_eq!(CP850.encode_lossy("text 🦀",231), bytes_undefined);

Using a trait object

use yore::CodePage;fndo_something(code_page:&dynCodePage,bytes:&[u8]){println!("{}", code_page.decode(bytes).unwrap());}

Supported code pages

IdentifierNameDescription
437ibm437OEM United States
737ibm737OEM Greek (formerly 437G); Greek (DOS)
775ibm775OEM Baltic; Baltic (DOS)
850ibm850OEM Multilingual Latin 1; Western European (DOS)
852ibm852OEM Latin 2; Central European (DOS)
855ibm855OEM Cyrillic (primarily Russian)
857ibm857OEM Turkish; Turkish (DOS)
860ibm860OEM Portuguese; Portuguese (DOS)
861ibm861OEM Icelandic; Icelandic (DOS)
862dos-862OEM Hebrew; Hebrew (DOS)
863ibm863OEM French Canadian; French Canadian (DOS)
864ibm864OEM Arabic; Arabic (864)
865ibm865OEM Nordic; Nordic (DOS)
866cp866OEM Russian; Cyrillic (DOS)
869ibm869OEM Modern Greek; Greek, Modern (DOS)
874windows-874Thai (Windows)
910ibm910IBM-PC APL2
1250windows-1250ANSI Central European; Central European (Windows)
1251windows-1251ANSI Cyrillic; Cyrillic (Windows)
1252windows-1252ANSI Latin 1; Western European (Windows)
1253windows-1253ANSI Greek; Greek (Windows)
1254windows-1254ANSI Turkish; Turkish (Windows)
1255windows-1255ANSI Hebrew; Hebrew (Windows)
1256windows-1256ANSI Arabic; Arabic (Windows)
1257windows-1257ANSI Baltic; Baltic (Windows)
1258windows-1258ANSI/OEM Vietnamese; Vietnamese (Windows)

* Benchmarks

encoding_rs supports only a few of the encodings that oem_cp and yore support. Additionally, encoding_rs focuses on streaming use cases.

Refer to the bench crate for more details.

no_std

yore has three feature tiers, so it scales from std down to bare-metal targets with no allocator:

Cargo featuresEnvironmentAPI
std (default)stdFull API + std::error::Error impls
allocno_std + allocatorFull API; Error impls omitted (Display stays)
(none)no_std, no allocatorAllocation-free char primitives only

The allocating encode/decode family returns owned Cow buffers and so requires the alloc feature. Without it, only the allocation-free encode_char / decode_byte primitives are available:

[dependencies]
# no_std with an allocator: keep the full Cow-returning APIyore = { version = "2.1.1", default-features = false, features = ["alloc"] }
# no_std without an allocator: char primitives onlyyore = { version = "2.1.1", default-features = false }
use yore::code_pages::CP850;// Encode/decode one character at a time, no allocation required.assert_eq!(CP850.encode_char('A'),Some(b'A'));assert_eq!(CP850.decode_byte(b'A'),'A');

CP437G (VGA text-mode glyphs)

The optional cp437g feature adds the CP437G code page: CP437 overlaid with the IBM-Graphics glyphs (☺ ♥ ♪ → ⌂ ...) at the C0 control byte range, as the VGA BIOS glyph ROM renders them. It pairs well with encode_char for driving a text buffer from a no_std kernel.

[dependencies]
yore = { version = "2.1.1", features = ["cp437g"] }
use yore::code_pages::CP437G;assert_eq!(CP437G.encode_char('☺'),Some(0x01));assert_eq!(CP437G.decode_byte(0x01),'☺');

Some glyphs share a byte with an ASCII control character (0x09 ○, 0x0A ◙, 0x0D ♪). The ASCII fast-path still encodes '\t'/'\n'/'\r' to those bytes, so intercept the source char first if you need newline semantics.

Contributing

See CONTRIBUTING.md for development setup, benchmarking, and fuzzing.

About

rust library for handling single byte character encodings like CP850 etc

Topics

Resources

Contributing

Stars

19 stars

Watchers

0 watching

Forks

Packages

Used by

Contributors

Languages