1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use super::{arc::ArcBuffer, number::Number};
use crate::type_information;
use std::{cmp::max, str::from_utf8_unchecked};

#[repr(C)]
#[derive(Clone, Debug, Default)]
pub struct ByteString {
    buffer: ArcBuffer,
}

impl ByteString {
    pub fn new(buffer: ArcBuffer) -> Self {
        Self { buffer }
    }

    pub fn empty() -> Self {
        Self {
            buffer: ArcBuffer::new(0),
        }
    }

    pub fn as_slice(&self) -> &[u8] {
        self.buffer.as_slice()
    }

    pub fn len(&self) -> usize {
        self.buffer.as_slice().len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn join(&self, other: &Self) -> Self {
        let mut buffer = ArcBuffer::new(self.len() + other.len());

        buffer.as_slice_mut()[..self.len()].copy_from_slice(self.as_slice());
        buffer.as_slice_mut()[self.len()..].copy_from_slice(other.as_slice());

        Self { buffer }
    }

    // Indices are inclusive and start from 1.
    pub fn slice(&self, start: Number, end: Number) -> ByteString {
        let start = f64::from(start);
        let end = f64::from(end);

        // TODO Allow infinite ranges
        if !start.is_finite() || !end.is_finite() {
            return Self::empty();
        }

        let start = max(start as isize - 1, 0) as usize;
        let end = max(end as isize, 0) as usize;

        let string = unsafe { from_utf8_unchecked(self.as_slice()) };

        if string.is_empty() || start >= string.chars().count() || end <= start {
            Self::empty()
        } else {
            string[Self::get_byte_index(string, start)..Self::get_byte_index(string, end)].into()
        }
    }

    fn get_byte_index(string: &str, index: usize) -> usize {
        string
            .char_indices()
            .nth(index)
            .map(|(index, _)| index)
            .unwrap_or_else(|| string.as_bytes().len())
    }
}

impl PartialEq for ByteString {
    fn eq(&self, other: &ByteString) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl From<&[u8]> for ByteString {
    fn from(bytes: &[u8]) -> Self {
        Self {
            buffer: bytes.into(),
        }
    }
}

impl From<&str> for ByteString {
    fn from(string: &str) -> Self {
        string.as_bytes().into()
    }
}

impl From<String> for ByteString {
    fn from(string: String) -> Self {
        string.as_str().into()
    }
}

impl From<Vec<u8>> for ByteString {
    fn from(vec: Vec<u8>) -> Self {
        vec.as_slice().into()
    }
}

type_information!(byte_string, crate::string::ByteString);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn join() {
        assert_eq!(
            ByteString::from("foo").join(&ByteString::from("bar")),
            ByteString::from("foobar")
        );
    }

    #[test]
    fn join_empty() {
        assert_eq!(
            ByteString::from("").join(&ByteString::from("")),
            ByteString::from("")
        );
    }

    #[test]
    fn slice_with_ascii() {
        assert_eq!(
            ByteString::from("abc").slice(2.0.into(), 2.0.into()),
            ByteString::from("b")
        );
    }

    #[test]
    fn slice_with_negative_index() {
        assert_eq!(
            ByteString::from("abc").slice((-1.0).into(), 3.0.into()),
            ByteString::from("abc")
        );
    }

    #[test]
    fn slice_into_whole() {
        assert_eq!(
            ByteString::from("abc").slice(1.0.into(), 3.0.into()),
            ByteString::from("abc")
        );
    }

    #[test]
    fn slice_into_empty() {
        assert_eq!(
            ByteString::from("abc").slice(4.0.into(), 4.0.into()),
            ByteString::from("")
        );
    }

    #[test]
    fn slice_with_emojis() {
        assert_eq!(
            ByteString::from("😀😉😂").slice(2.0.into(), 2.0.into()),
            ByteString::from("😉")
        );
    }

    #[test]
    fn slice_last_with_emojis() {
        assert_eq!(
            ByteString::from("😀😉😂").slice(3.0.into(), 3.0.into()),
            ByteString::from("😂")
        );
    }
}