use std::borrow::{Borrow, Cow};
use std::hash::{Hash, Hasher};
use std::ops::{self, Range};
use std::str::{self, FromStr};
use std::{cmp, fmt, slice};
pub struct Latin1String {
bytes: Vec<u8>,
}
impl Latin1String {
#[must_use]
pub fn new() -> Latin1String {
Latin1String { bytes: Vec::new() }
}
#[must_use]
pub fn with_capacity(capacity: usize) -> Latin1String {
Latin1String {
bytes: Vec::with_capacity(capacity),
}
}
#[must_use]
pub fn as_latin1_str(&self) -> &Latin1Str {
self
}
#[must_use]
pub fn as_latin1_str_mut(&mut self) -> &mut Latin1Str {
self
}
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_bytes(self) -> Vec<u8> {
self.bytes
}
pub fn clear(&mut self) {
self.bytes.clear()
}
pub fn from_utf8(string: &str) -> Result<Latin1String, Utf8ToLatin1Error> {
let bytes = string.as_bytes();
let mut latin1_bytes = Vec::with_capacity(string.len());
let mut i = 0;
let mut pos = 0;
while i < bytes.len() {
let byte = bytes[i];
let mut error = false;
if byte < 128 {
latin1_bytes.push(byte);
i += 1;
} else if byte == 0xc2 {
let next_byte = bytes[i + 1];
if (128..192).contains(&next_byte) {
latin1_bytes.push(next_byte);
i += 2;
} else {
error = true;
}
} else if byte == 0xc3 {
let next_byte = bytes[i + 1];
if (128..192).contains(&next_byte) {
latin1_bytes.push(next_byte + 64);
i += 2;
} else {
error = true;
}
} else {
error = true;
}
if error {
let value = string[i..].chars().next().unwrap();
return Err(Utf8ToLatin1Error { pos, value });
}
pos += 1;
}
Ok(Latin1String::from(latin1_bytes))
}
#[cfg(test)]
pub fn from_utf8_unchecked(string: &str) -> Latin1String {
Self::from_utf8(string).unwrap()
}
pub fn push(&mut self, byte: u8) {
self.bytes.push(byte)
}
pub fn append(&mut self, other: &mut Latin1String) {
self.bytes.append(&mut other.bytes);
}
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_boxed_latin1_str(self) -> Box<Latin1Str> {
let rw = Box::into_raw(self.bytes.into_boxed_slice()) as *mut Latin1Str;
unsafe { Box::from_raw(rw) }
}
}
impl Clone for Latin1String {
fn clone(&self) -> Self {
Self {
bytes: self.bytes.clone(),
}
}
fn clone_from(&mut self, source: &Self) {
self.bytes.clone_from(&source.bytes)
}
}
impl From<&Latin1Str> for Box<Latin1Str> {
fn from(value: &Latin1Str) -> Self {
value.to_boxed()
}
}
impl From<&mut Latin1Str> for Box<Latin1Str> {
fn from(value: &mut Latin1Str) -> Self {
Self::from(&*value)
}
}
impl AsRef<Latin1Str> for Latin1String {
fn as_ref(&self) -> &Latin1Str {
self.as_latin1_str()
}
}
impl From<Box<Latin1Str>> for Latin1String {
fn from(value: Box<Latin1Str>) -> Self {
value.into_latin1_string()
}
}
impl From<Latin1String> for Box<Latin1Str> {
fn from(value: Latin1String) -> Self {
value.into_boxed_latin1_str()
}
}
impl From<&[u8]> for Box<Latin1Str> {
fn from(value: &[u8]) -> Self {
Latin1String::from(value).into_boxed_latin1_str()
}
}
impl<const N: usize> From<&[u8; N]> for Box<Latin1Str> {
fn from(value: &[u8; N]) -> Self {
Latin1String::from(value).into_boxed_latin1_str()
}
}
impl Clone for Box<Latin1Str> {
fn clone(&self) -> Self {
self.to_latin1_string().into_boxed_latin1_str()
}
}
impl<T: ?Sized + AsRef<[u8]>> From<&T> for Latin1String {
fn from(s: &T) -> Latin1String {
Latin1String::from(s.as_ref().to_vec())
}
}
impl From<Vec<u8>> for Latin1String {
fn from(value: Vec<u8>) -> Self {
Latin1String { bytes: value }
}
}
impl From<u8> for Latin1String {
fn from(value: u8) -> Self {
Latin1String { bytes: vec![value] }
}
}
impl From<Latin1String> for Vec<u8> {
fn from(value: Latin1String) -> Self {
value.bytes
}
}
impl From<&Latin1Str> for Latin1String {
fn from(value: &Latin1Str) -> Self {
value.to_owned()
}
}
impl FromStr for Latin1String {
type Err = Utf8ToLatin1Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Latin1String::from_utf8(s)
}
}
impl FromIterator<u8> for Latin1String {
fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
Latin1String::from(iter.into_iter().collect::<Vec<_>>())
}
}
impl Extend<u8> for Latin1String {
fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
self.bytes.extend(iter);
}
}
impl<'a> Extend<&'a u8> for Latin1String {
fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, iter: T) {
self.bytes.extend(iter);
}
}
impl fmt::Debug for Latin1String {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl ops::Deref for Latin1String {
type Target = Latin1Str;
fn deref(&self) -> &Self::Target {
Latin1Str::new(self.bytes.as_ref())
}
}
impl ops::DerefMut for Latin1String {
fn deref_mut(&mut self) -> &mut Self::Target {
Latin1Str::new_mut(&mut self.bytes)
}
}
impl Borrow<Latin1Str> for Latin1String {
fn borrow(&self) -> &Latin1Str {
self
}
}
impl Default for Latin1String {
fn default() -> Self {
Latin1String::new()
}
}
impl ToOwned for Latin1Str {
type Owned = Latin1String;
fn to_owned(&self) -> Self::Owned {
self.to_latin1_string()
}
fn clone_into(&self, target: &mut Latin1String) {
self.inner.clone_into(&mut target.bytes);
}
}
impl cmp::PartialEq for Latin1String {
fn eq(&self, other: &Self) -> bool {
self.as_latin1_str() == other.as_latin1_str()
}
}
impl Hash for Latin1String {
fn hash<H: Hasher>(&self, h: &mut H) {
self.as_latin1_str().hash(h)
}
}
impl Eq for Latin1String {}
impl cmp::PartialEq<str> for Latin1String {
fn eq(&self, other: &str) -> bool {
cmp_latin1_to_str(self.as_latin1_str(), other)
}
}
impl cmp::PartialEq<String> for Latin1String {
fn eq(&self, other: &String) -> bool {
cmp_latin1_to_str(self.as_latin1_str(), other.as_str())
}
}
impl cmp::PartialEq<Latin1String> for str {
fn eq(&self, other: &Latin1String) -> bool {
cmp_latin1_to_str(other.as_latin1_str(), self)
}
}
impl cmp::PartialEq<Latin1String> for String {
fn eq(&self, other: &Latin1String) -> bool {
cmp_latin1_to_str(other.as_latin1_str(), self.as_str())
}
}
impl PartialOrd for Latin1String {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Latin1String {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.as_latin1_str().cmp(other.as_latin1_str())
}
}
impl AsRef<[u8]> for Latin1String {
fn as_ref(&self) -> &[u8] {
&self.bytes[..]
}
}
impl AsRef<Latin1Str> for [u8] {
fn as_ref(&self) -> &Latin1Str {
Latin1Str::new(self)
}
}
impl<const N: usize> AsRef<Latin1Str> for [u8; N] {
fn as_ref(&self) -> &Latin1Str {
Latin1Str::new(self)
}
}
impl fmt::Display for Latin1String {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[derive(PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Latin1Str {
inner: [u8],
}
pub fn latin1_char_lowercased(chr: u8) -> u8 {
match chr {
b'A'..=b'Z' | 0xC0..=0xD6 | 0xD8..=0xDE => chr + 32,
_ => chr,
}
}
pub fn latin1_char_uppercased(chr: u8) -> u8 {
match chr {
b'a'..=b'z' | 0xE0..=0xF6 | 0xF8..=0xFE => chr - 32,
_ => chr,
}
}
pub struct Chars<'a> {
iter: slice::Iter<'a, u8>,
}
impl<'a> Chars<'a> {
pub fn new(value: &'a Latin1Str) -> Chars<'a> {
Chars {
iter: value.inner.iter(),
}
}
}
impl<'a> Iterator for Chars<'a> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|byte| *byte as char)
}
fn count(self) -> usize
where
Self: Sized,
{
self.iter.count()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
self.iter.last().map(|byte| *byte as char)
}
}
impl<'a> ExactSizeIterator for Chars<'a> {}
impl<'a> DoubleEndedIterator for Chars<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(|byte| *byte as char)
}
}
impl Latin1Str {
pub fn new(bytes: &[u8]) -> &Latin1Str {
unsafe { &*(bytes as *const [u8] as *const Latin1Str) }
}
pub fn new_mut(bytes: &mut [u8]) -> &mut Latin1Str {
unsafe { &mut *(bytes as *mut [u8] as *mut Latin1Str) }
}
pub fn as_bytes(&self) -> &[u8] {
&self.inner
}
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
&mut self.inner
}
pub fn to_latin1_string(&self) -> Latin1String {
Latin1String::from(&self.inner)
}
pub fn chars(&self) -> Chars<'_> {
Chars::new(self)
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn make_lowercase(&mut self) {
for i in 0..self.inner.len() {
self.inner[i] = latin1_char_lowercased(self.inner[i]);
}
}
pub fn make_uppercase(&mut self) {
for i in 0..self.inner.len() {
self.inner[i] = latin1_char_uppercased(self.inner[i]);
}
}
pub fn to_lowercase(&self) -> Latin1String {
let mut latin1 = self.to_owned();
latin1.make_lowercase();
latin1
}
pub fn to_uppercase(&self) -> Latin1String {
let mut latin1 = self.to_owned();
latin1.make_uppercase();
latin1
}
pub fn starts_with(&self, other: &Latin1String) -> bool {
if other.len() <= self.len() {
self.inner[0..other.len()] == other.bytes
} else {
false
}
}
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_latin1_string(self: Box<Self>) -> Latin1String {
let rw = Box::into_raw(self) as *mut [u8];
let inner = unsafe { Box::from_raw(rw) };
Latin1String {
bytes: Vec::from(inner),
}
}
pub unsafe fn to_str_unchecked(&self) -> &str {
debug_assert!(
self.is_utf8(),
"called to_str_unchecked with non-ascii bytes"
);
str::from_utf8_unchecked(&self.inner)
}
pub fn is_utf8(&self) -> bool {
self.inner.is_ascii()
}
pub fn to_str(&self) -> Cow<'_, str> {
if self.is_utf8() {
Cow::Borrowed(unsafe { self.to_str_unchecked() })
} else {
Cow::Owned(iso_8859_1_to_utf8(&self.inner))
}
}
pub fn to_boxed(&self) -> Box<Latin1Str> {
let boxed: Box<[u8]> = self.inner.into();
let raw = Box::into_raw(boxed) as *mut Latin1Str;
unsafe { Box::from_raw(raw) }
}
pub fn eq_ignore_case(&self, other: impl AsRef<Latin1Str>) -> bool {
self.to_lowercase() == other.as_ref().to_lowercase()
}
pub fn neq_ignore_case(&self, other: impl AsRef<Latin1Str>) -> bool {
self.to_lowercase() != other.as_ref().to_lowercase()
}
}
fn iso_8859_1_to_utf8(bytes: &[u8]) -> String {
let mut utf8_bytes = Vec::with_capacity(bytes.len());
for byte in bytes.iter().copied() {
if byte < 128 {
utf8_bytes.push(byte);
} else if byte < 192 {
utf8_bytes.push(0xc2);
utf8_bytes.push(byte);
} else {
utf8_bytes.push(0xc3);
utf8_bytes.push(byte - 64);
}
}
unsafe { String::from_utf8_unchecked(utf8_bytes) }
}
impl fmt::Debug for Latin1Str {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&iso_8859_1_to_utf8(&self.inner), f)
}
}
impl fmt::Display for Latin1Str {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&iso_8859_1_to_utf8(&self.inner), f)
}
}
impl AsRef<Latin1Str> for Latin1Str {
fn as_ref(&self) -> &Latin1Str {
self
}
}
impl<const N: usize> From<[u8; N]> for Latin1String {
fn from(value: [u8; N]) -> Self {
Self {
bytes: value.into(),
}
}
}
impl std::ops::Index<Range<usize>> for Latin1String {
type Output = Latin1Str;
fn index(&self, index: Range<usize>) -> &Self::Output {
Latin1Str::new(self.bytes.index(index))
}
}
impl std::ops::Index<usize> for Latin1Str {
type Output = u8;
fn index(&self, index: usize) -> &Self::Output {
&self.inner[index]
}
}
impl std::ops::IndexMut<usize> for Latin1Str {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.inner[index]
}
}
#[allow(clippy::cmp_owned)]
fn cmp_latin1_to_str(latin1: &Latin1Str, str: &str) -> bool {
latin1.to_string() == str
}
impl cmp::PartialEq<str> for Latin1Str {
fn eq(&self, other: &str) -> bool {
cmp_latin1_to_str(self, other)
}
}
impl cmp::PartialEq<String> for Latin1Str {
fn eq(&self, other: &String) -> bool {
cmp_latin1_to_str(self, other.as_str())
}
}
impl cmp::PartialEq<Latin1Str> for str {
fn eq(&self, other: &Latin1Str) -> bool {
cmp_latin1_to_str(other, self)
}
}
impl cmp::PartialEq<Latin1Str> for String {
fn eq(&self, other: &Latin1Str) -> bool {
cmp_latin1_to_str(other, self.as_str())
}
}
impl<'a> IntoIterator for &'a Latin1Str {
type Item = &'a u8;
type IntoIter = slice::Iter<'a, u8>;
fn into_iter(self) -> Self::IntoIter {
self.inner.iter()
}
}
#[derive(PartialEq, Eq, Debug)]
pub struct Utf8ToLatin1Error {
pub pos: usize,
pub value: char,
}
impl fmt::Display for Utf8ToLatin1Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Invalid latin-1 character '{}' at position {}",
self.value, self.pos
)
}
}
impl std::error::Error for Utf8ToLatin1Error {}
pub fn char_to_latin1(chr: char) -> Result<u8, NonLatin1CharError> {
let mut bytes = [0; 4];
chr.encode_utf8(&mut bytes);
let byte = bytes[0];
if byte < 128 {
Ok(byte)
} else if byte == 0xc2 {
let next_byte = bytes[1];
if (128..192).contains(&next_byte) {
Ok(next_byte)
} else {
Err(NonLatin1CharError { ch: chr })
}
} else if byte == 0xc3 {
let next_byte = bytes[1];
if (128..192).contains(&next_byte) {
Ok(next_byte + 64)
} else {
Err(NonLatin1CharError { ch: chr })
}
} else {
Err(NonLatin1CharError { ch: chr })
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct NonLatin1CharError {
pub ch: char,
}
impl fmt::Display for NonLatin1CharError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"character '{}' (U+{:04X}) cannot be represented in Latin-1",
self.ch, self.ch as u32
)
}
}
impl std::error::Error for NonLatin1CharError {}
pub trait ToLatin1 {
fn to_latin1(&self) -> Latin1String;
}
#[cfg(test)]
mod tests {
use super::*;
use assert_matches::assert_matches;
#[test]
fn latin1_to_utf8() {
for byte in 0..=255 {
let latin1 = Latin1String::from(&[byte]);
assert_eq!(
Latin1String::from_utf8(&latin1.to_string()).unwrap(),
latin1
)
}
}
#[test]
fn latin1_lowercase() {
for byte in 0..=255u8 {
let latin1 = Latin1String::from(&[byte]);
let utf8 = latin1.to_string();
assert_eq!(latin1.to_lowercase().to_string(), utf8.to_lowercase());
}
}
#[test]
fn utf8_to_latin1() {
let utf8 = "åäö";
assert_matches!(Latin1String::from_utf8(utf8), Ok(latin1) => {
assert_eq!(latin1.bytes, [229, 228, 246]);
assert_eq!(latin1.to_string(), utf8);
})
}
#[test]
fn utf8_to_latin1_error() {
let utf8 = "abö€";
assert_matches!(Latin1String::from_utf8(utf8), Err(err) => {
assert_eq!(err.pos, 3);
assert_eq!(err.value, '€');
});
let utf8 = "a\nbö\n€";
assert_matches!(Latin1String::from_utf8(utf8), Err(err) => {
assert_eq!(err.pos, 5);
assert_eq!(err.value, '€');
assert_eq!(format!("{}", err), "Invalid latin-1 character '€' at position 5");
});
}
#[test]
fn compare_latin1_to_utf8() {
let utf8 = "café";
let latin1 = Latin1String::from_utf8("café").unwrap();
assert!(utf8.as_bytes() != latin1.as_bytes());
assert!(latin1.as_latin1_str() == utf8);
}
}