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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! Encoding and decoding functions for compressed values.
//!
//! This module provides internal functions for encoding JSON values into
//! their compressed string representations and decoding them back.
//!
//! # Encoding Format
//!
//! | Type | Prefix | Example |
//! |------|--------|---------|
//! | Boolean true | `b\|T` | `"b\|T"` |
//! | Boolean false | `b\|F` | `"b\|F"` |
//! | Number | `n\|` | `"n\|42.5"` |
//! | Infinity | `N\|+` | `"N\|+"` (when preserved) |
//! | -Infinity | `N\|-` | `"N\|-"` (when preserved) |
//! | NaN | `N\|0` | `"N\|0"` (when preserved) |
//! | Escaped string | `s\|` | `"s\|n\|foo"` |
//! | Plain string | _(none)_ | `"hello"` |
//!
//! # String Escaping
//!
//! Strings that start with reserved prefixes (`b|`, `n|`, `N|`, `o|`, `a|`, `s|`)
//! are escaped with `s|` to prevent ambiguity during decoding.
//!
//! # Special Values (v3.4.0+)
//!
//! Special floating-point values have dedicated encodings when enabled via config:
//! - `Infinity` → `N|+` (when `preserve_infinite` is true)
//! - `-Infinity` → `N|-` (when `preserve_infinite` is true)
//! - `NaN` → `N|0` (when `preserve_nan` is true)
//!
//! When preservation is disabled (default), special values become `null` like `JSON.stringify`.
//! This ensures compatibility with JavaScript and Python implementations v3.4.0+.
use crates_to_int;
/// Encode a regular number to compressed string with 'n|' prefix.
///
/// This function is for regular (finite) numbers only. Special values
/// (Infinity, -Infinity, NaN) are handled separately in `memory.rs`
/// based on configuration settings.
///
/// # Arguments
///
/// * `num` - The f64 number to encode (should be finite)
///
/// # Returns
///
/// String in format `"n|<number>"`
///
/// # Example
///
/// ```ignore
/// assert_eq!(encode_num(42.5), "n|42.5");
/// assert_eq!(encode_num(-3.14), "n|-3.14");
/// assert_eq!(encode_num(0.0), "n|0");
/// ```
///
/// # Note
///
/// For special values (Infinity, NaN), the handling depends on config:
/// - `preserve_nan`/`preserve_infinite`: encoded as `N|0`, `N|+`, `N|-`
/// - Otherwise: converted to null (empty string)
/// Check if an encoded string represents a special value (Infinity/NaN).
///
/// # Arguments
///
/// * `s` - The encoded string to check
///
/// # Returns
///
/// `true` if the string starts with `N|` (special value prefix)
/// Decode a special value string to f64.
///
/// # Arguments
///
/// * `s` - String starting with "N|" prefix
///
/// # Returns
///
/// - `f64::INFINITY` for "N|+"
/// - `f64::NEG_INFINITY` for "N|-"
/// - `f64::NAN` for "N|0"
///
/// # Panics
///
/// Panics if the string is not a valid special value encoding.
/// Decode a compressed number string to f64.
///
/// # Arguments
///
/// * `s` - String starting with "n|" prefix
///
/// # Returns
///
/// The decoded f64 value
///
/// # Panics
///
/// Panics if the string after the prefix is not a valid number.
/// Decode a key string (base-62) to an index.
///
/// Converts a base-62 encoded key back to its numeric index
/// in the values array.
///
/// # Arguments
///
/// * `key` - Base-62 encoded key string
///
/// # Returns
///
/// The numeric index as usize
/// Encode a boolean to compressed string with 'b|' prefix.
///
/// # Arguments
///
/// * `b` - Boolean value to encode
///
/// # Returns
///
/// `"b|T"` for true, `"b|F"` for false
/// Decode a compressed boolean string to bool.
///
/// # Arguments
///
/// * `s` - String "b|T" or "b|F"
///
/// # Returns
///
/// `true` for "b|T", `false` for "b|F" or empty string
/// Encode a string, escaping reserved prefixes with 's|' if needed.
///
/// If the string starts with a reserved prefix (`b|`, `o|`, `n|`, `N|`, `a|`, `s|`),
/// it's escaped by prepending `s|` to prevent decoding ambiguity.
///
/// # Arguments
///
/// * `s` - The string to encode
///
/// # Returns
///
/// The original string, or escaped with `s|` prefix if needed
///
/// # Example
///
/// ```ignore
/// assert_eq!(encode_str("hello"), "hello");
/// assert_eq!(encode_str("n|123"), "s|n|123"); // Escaped
/// assert_eq!(encode_str("N|+"), "s|N|+"); // Escaped (v3.2.0)
/// ```
/// Decode a compressed string, unescaping 's|' prefix if present.
///
/// # Arguments
///
/// * `s` - The encoded string
///
/// # Returns
///
/// The original string with `s|` prefix removed if present