ignite/pstring/
pstring.rs1use super::compressor::Compressor;
2use super::COMPRESSIONDEFAULT;
3use snap;
4use std::fmt;
5use std::str;
6use std::string::String;
7use std::vec::Vec;
8use str_util::to_bytes;
9
10#[derive(Debug, Clone)]
13pub struct PString {
14 pub compression: bool,
15 codepoints: Vec<u8>,
16}
17
18impl fmt::Display for PString {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 write!(f, "{}", self.clone().to_string_backend())
22 }
23}
24
25impl PString {
26 #[inline]
28 pub fn new() -> PString {
29 PString {
30 compression: COMPRESSIONDEFAULT,
31 codepoints: Vec::new(),
32 }
33 }
34
35 #[inline]
37 pub fn char_at(&self, index: usize) -> char {
38 let bytes = self.decompress_to_bytes();
39
40 bytes[index] as char
41 }
42
43 #[inline]
45 pub fn with_capacity(capacity: usize) -> PString {
46 PString {
47 compression: COMPRESSIONDEFAULT,
48 codepoints: Vec::with_capacity(capacity),
49 }
50 }
51
52 #[inline]
54 pub fn from_bytes(codepoints: Vec<u8>) -> PString {
55 PString {
56 compression: COMPRESSIONDEFAULT,
57 codepoints: Compressor::compress_bytes(codepoints, COMPRESSIONDEFAULT),
58 }
59 }
60
61 #[inline]
63 pub fn set_bytes(&mut self, codepoints: Vec<u8>) {
64 self.codepoints = Compressor::compress_bytes(codepoints, self.compression);
65 }
66
67 #[inline]
69 pub fn set_str(&mut self, string: &'static str) {
70 self.codepoints = Compressor::compress_bytes(to_bytes::str_to_bytes(string), self.compression);
71 }
72
73 #[inline]
75 pub fn set_string(&mut self, string: String) {
76 self.codepoints = Compressor::compress_bytes(to_bytes::string_to_bytes(string), self.compression);
77 }
78
79 #[inline]
81 pub fn from_str(string: &'static str) -> PString {
82 let returnvalue: PString = PString::from_bytes(to_bytes::str_to_bytes(string));
83
84 returnvalue
85 }
86
87 #[inline]
89 pub fn from_string(string: String) -> PString {
90 let returnvalue: PString = PString::from_bytes(to_bytes::string_to_bytes(string));
91
92 returnvalue
93 }
94
95 #[inline]
97 fn to_string_backend(&mut self) -> String {
98 let bytes = Compressor::decompress_bytes(self.codepoints.clone(), self.compression);
99
100 String::from_utf8(bytes).unwrap()
101 }
102
103 #[inline]
105 pub fn as_bytes(&mut self) -> Vec<u8> {
106 self.decompress_to_bytes()
107 }
108
109 #[inline]
111 pub fn as_bytes_raw(&self) -> Vec<u8> {
112 self.codepoints.clone()
113 }
114
115 pub fn chars(&self) -> Vec<char> {
117 let mut chararray: Vec<char> = Vec::new();
118 for byte in self.decompress_to_bytes() {
119 chararray.push(byte as char);
120 }
121
122 chararray
123 }
124
125 pub fn compress(&mut self) {
127 if self.compression == false {
128 let mut encoder = snap::Encoder::new();
129 self.codepoints = encoder.compress_vec(&self.codepoints[..]).unwrap();
130 self.compression = true;
131 }
132 }
133
134 pub fn decompress(&mut self) {
136 if self.compression == true {
137 let mut decoder = snap::Decoder::new();
138 self.codepoints = decoder.decompress_vec(&self.codepoints[..]).unwrap();
139 self.compression = false;
140 }
141 }
142
143 #[allow(dead_code)]
145 #[inline(always)]
146 fn compress_to_bytes(&self) -> Vec<u8> {
147 if self.compression == true {
148 let mut encoder = snap::Encoder::new();
149 return encoder.compress_vec(&self.codepoints[..]).unwrap();
150 }
151
152 self.codepoints.clone()
153 }
154
155 #[allow(dead_code)]
157 #[inline(always)]
158 fn decompress_to_bytes(&self) -> Vec<u8> {
159 if self.compression == true {
160 let mut decoder = snap::Decoder::new();
161 return decoder.decompress_vec(&self.codepoints[..]).unwrap();
162 }
163
164 self.codepoints.clone()
165 }
166
167 #[allow(dead_code)]
169 #[inline(always)]
170 fn autocompress(&mut self) {
171 if self.compression {
172 let mut encoder = snap::Encoder::new();
173 self.codepoints = encoder.compress_vec(&self.codepoints[..]).unwrap();
174 }
175 }
176
177 #[allow(dead_code)]
179 #[inline(always)]
180 fn autodecompress(&mut self) {
181 if self.compression {
182 let mut decoder = snap::Decoder::new();
183 self.codepoints = decoder.decompress_vec(&self.codepoints[..]).unwrap();
184 }
185 }
186}