7 releases

new 0.1.6 Apr 24, 2025
0.1.5 Apr 24, 2025
0.1.2 Dec 9, 2024
0.1.1 May 31, 2022

#1 in #binary-string

Download history 21876/week @ 2025-01-02 29019/week @ 2025-01-09 27776/week @ 2025-01-16 28065/week @ 2025-01-23 26102/week @ 2025-01-30 29550/week @ 2025-02-06 29295/week @ 2025-02-13 32425/week @ 2025-02-20 32036/week @ 2025-02-27 34935/week @ 2025-03-06 33247/week @ 2025-03-13 23974/week @ 2025-03-20 19618/week @ 2025-03-27 22291/week @ 2025-04-03 20262/week @ 2025-04-10 15544/week @ 2025-04-17

81,580 downloads per month
Used in 38 crates (2 directly)

MIT license

22KB
397 lines

binstring

A Rust library for storing binary data as a string.

Crates.io Documentation License: MIT

Description

binstring provides a BinString type that wraps a String and provides conversion methods between binary data and strings. It's particularly useful when you need to store binary data in a string format while maintaining the ability to access it as both a string and bytes.

Installation

Add this to your Cargo.toml:

[dependencies]
binstring = "0.1"

Usage

Basic Usage

use binstring::BinString;

// Create from a string
let bin_str = BinString::new("hello");
assert_eq!(bin_str.as_str(), "hello");

// Create from bytes
let bytes = vec![104, 101, 108, 108, 111]; // "hello" in bytes
let bin_str = BinString::from_bytes(bytes);
assert_eq!(bin_str.as_bytes(), &[104, 101, 108, 108, 111]);

Converting Between Types

use binstring::BinString;

// From String
let s = String::from("hello");
let bin_str = BinString::from(s);

// From &str
let bin_str = BinString::from("hello");

// From Vec<u8>
let bytes = vec![104, 101, 108, 108, 111];
let bin_str = BinString::from(bytes);

// From &[u8]
let bytes = &[104, 101, 108, 108, 111];
let bin_str = BinString::from(bytes);

Accessing Data

use binstring::BinString;

let bin_str = BinString::new("hello");

// As string slice (only safe with valid UTF-8)
assert_eq!(bin_str.as_str(), "hello");

// As byte slice (always safe)
assert_eq!(bin_str.as_bytes(), &[104, 101, 108, 108, 111]);

// Get length
assert_eq!(bin_str.len(), 5);

// Check if empty
assert!(!bin_str.is_empty());

Byte-Level Operations

use binstring::BinString;

let s = BinString::new("hello");

// Concatenation
let s2 = BinString::new(" world");
assert_eq!(s.concat(&s2).as_bytes(), b"hello world");

// Slicing
assert_eq!(s.slice(1..4).as_bytes(), b"ell");

// Pattern matching
assert!(s.starts_with(b"he"));
assert!(s.ends_with(b"lo"));
assert!(s.contains(b"el"));
assert_eq!(s.find(b"el"), Some(1));
assert_eq!(s.rfind(b"l"), Some(3));

// Byte replacement
assert_eq!(s.replace(b'l', b'x').as_bytes(), b"hexxo");

Converting Back

use binstring::BinString;

let bin_str = BinString::new("hello");

// Get the underlying String
let s = bin_str.unwrap();
assert_eq!(s, "hello");

// Or use From trait
let s: String = bin_str.into();
assert_eq!(s, "hello");

Safety

While storing invalid UTF-8 in a String is perfectly safe, attempting to treat the string as valid UTF-8 (for example, by displaying it or using string operations that assume valid UTF-8) may lead to undefined behavior.

Most operations in this library work at the byte level and are safe to use with any binary data. However, some methods like trim() assume valid UTF-8 and should only be used when you are certain the data is valid UTF-8.

No runtime deps