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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use crate::filter::{Env, Filter};
use crate::value::Value;
use std::sync::Arc;
use super::super::eval::eval;
use super::super::value_ops::values_equal;
use super::set_error;
/// Truncate a string to at most `max` bytes, ensuring the cut falls on a UTF-8
/// char boundary. Returns the full string if it's already short enough.
fn safe_truncate(s: &str, max: usize) -> &str {
if s.len() <= max {
return s;
}
let mut end = max;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
pub(super) fn eval_strings(
name: &str,
args: &[Filter],
input: &Value,
env: &Env,
output: &mut dyn FnMut(Value),
) {
match name {
"tostring" => match input {
Value::String(_) => output(input.clone()),
Value::Int(n) => output(Value::String(itoa::Buffer::new().format(*n).into())),
Value::Double(_, _) => {
// Use write_compact for consistent formatting (integer expansion, etc.)
let mut buf = Vec::new();
crate::output::write_compact(&mut buf, input, false).unwrap();
output(Value::String(String::from_utf8(buf).unwrap_or_default()));
}
Value::Bool(b) => output(Value::String(if *b { "true" } else { "false" }.into())),
Value::Null => output(Value::String("null".into())),
Value::Array(_) | Value::Object(_) => {
let mut buf = Vec::new();
crate::output::write_compact(&mut buf, input, false).unwrap();
output(Value::String(String::from_utf8(buf).unwrap_or_default()));
}
},
"tonumber" => match input {
Value::Int(_) | Value::Double(..) => output(input.clone()),
Value::String(s) => {
if let Ok(n) = s.parse::<i64>() {
output(Value::Int(n));
} else if let Ok(f) = s.parse::<f64>() {
output(Value::Double(f, None));
} else {
set_error(format!(
"string ({}) cannot be parsed as a number",
Value::String(s.clone()).short_desc()
));
}
}
_ => {
set_error(format!(
"{} ({}) cannot be parsed as a number",
input.type_name(),
input.short_desc()
));
}
},
"ascii_downcase" => {
if let Value::String(s) = input {
output(Value::String(
s.chars().map(|c| c.to_ascii_lowercase()).collect(),
));
} else if !matches!(input, Value::Null) {
set_error(format!(
"{} ({}) cannot be ascii_downcased",
input.type_name(),
input.short_desc()
));
}
}
"ascii_upcase" => {
if let Value::String(s) = input {
output(Value::String(
s.chars().map(|c| c.to_ascii_uppercase()).collect(),
));
} else if !matches!(input, Value::Null) {
set_error(format!(
"{} ({}) cannot be ascii_upcased",
input.type_name(),
input.short_desc()
));
}
}
"ltrimstr" => {
if let Some(arg) = args.first() {
let mut prefix = Value::Null;
eval(arg, input, env, &mut |v| prefix = v);
match (input, &prefix) {
(Value::String(s), Value::String(p)) => {
output(Value::String(
s.strip_prefix(p.as_str()).unwrap_or(s).to_string(),
));
}
_ => {
set_error("startswith() requires string inputs".to_string());
}
}
}
}
"rtrimstr" => {
if let Some(arg) = args.first() {
let mut suffix = Value::Null;
eval(arg, input, env, &mut |v| suffix = v);
match (input, &suffix) {
(Value::String(s), Value::String(p)) => {
output(Value::String(
s.strip_suffix(p.as_str()).unwrap_or(s).to_string(),
));
}
_ => {
set_error("endswith() requires string inputs".to_string());
}
}
}
}
"trimstr" => {
if let (Value::String(s), Some(arg)) = (input, args.first()) {
let mut pat = Value::Null;
eval(arg, input, env, &mut |v| pat = v);
if let Value::String(p) = pat {
let trimmed = s.strip_prefix(p.as_str()).unwrap_or(s);
output(Value::String(
trimmed
.strip_suffix(p.as_str())
.unwrap_or(trimmed)
.to_string(),
));
} else {
output(input.clone());
}
} else if !args.is_empty() && !matches!(input, Value::String(_)) {
set_error(format!(
"{} ({}) and string cannot have their strings trimmed",
input.type_name(),
input.short_desc()
));
}
}
"startswith" => {
if let Some(arg) = args.first() {
let mut prefix = Value::Null;
eval(arg, input, env, &mut |v| prefix = v);
match (input, &prefix) {
(Value::String(s), Value::String(p)) => {
output(Value::Bool(s.starts_with(p.as_str())));
}
_ => {
set_error("startswith() requires string inputs".to_string());
}
}
}
}
"endswith" => {
if let Some(arg) = args.first() {
let mut suffix = Value::Null;
eval(arg, input, env, &mut |v| suffix = v);
match (input, &suffix) {
(Value::String(s), Value::String(p)) => {
output(Value::Bool(s.ends_with(p.as_str())));
}
_ => {
set_error("endswith() requires string inputs".to_string());
}
}
}
}
"split" => {
if let (Value::String(s), Some(arg)) = (input, args.first()) {
let mut sep = Value::Null;
eval(arg, input, env, &mut |v| sep = v);
if let Value::String(p) = sep {
let parts: Vec<Value> = if p.is_empty() {
s.chars().map(|c| Value::String(c.to_string())).collect()
} else {
s.split(p.as_str())
.map(|part| Value::String(part.into()))
.collect()
};
output(Value::Array(Arc::new(parts)));
}
} else if !args.is_empty() && !matches!(input, Value::String(_)) {
set_error(format!(
"{} ({}) cannot be split",
input.type_name(),
input.short_desc()
));
}
}
"join" => {
if let (Value::Array(arr), Some(arg)) = (input, args.first()) {
eval(arg, input, env, &mut |sep| {
if let Value::String(p) = sep {
let mut result = String::new();
let mut first = true;
for v in arr.iter() {
if !first {
result.push_str(&p);
}
first = false;
match v {
Value::String(s) => result.push_str(s),
Value::Int(n) => result.push_str(itoa::Buffer::new().format(*n)),
Value::Double(f, _) => {
result.push_str(ryu::Buffer::new().format(*f))
}
Value::Null => {}
Value::Bool(b) => {
result.push_str(if *b { "true" } else { "false" })
}
_ => {
// jq error: string ("partial,") and object ({...}) cannot be added
let partial_desc = if result.len() > 10 {
format!(
"\"{}...",
result.chars().take(10).collect::<String>()
)
} else {
format!("\"{result}\"")
};
set_error(format!(
"string ({}) and {} ({}) cannot be added",
partial_desc,
v.type_name(),
v.short_desc()
));
return;
}
}
}
output(Value::String(result));
}
});
}
}
"trim" | "ltrim" | "rtrim" => {
if let Value::String(s) = input {
let trimmed = match name {
"ltrim" => s.trim_start(),
"rtrim" => s.trim_end(),
_ => s.trim(),
};
output(Value::String(trimmed.to_string()));
} else {
set_error("trim input must be a string".to_string());
}
}
"index" => {
if let Some(arg) = args.first() {
// Generator: produce one result per arg value
eval(arg, input, env, &mut |needle| match (input, &needle) {
(Value::String(s), Value::String(n)) => {
if n.is_empty() {
output(Value::Null);
} else if let Some(byte_pos) = s.find(n.as_str()) {
output(Value::Int(s[..byte_pos].chars().count() as i64));
} else {
output(Value::Null);
}
}
(Value::Array(arr), _) => {
let pos = arr.iter().position(|v| values_equal(v, &needle));
match pos {
Some(i) => output(Value::Int(i as i64)),
None => output(Value::Null),
}
}
_ => output(Value::Null),
});
}
}
"rindex" => {
if let Some(arg) = args.first() {
eval(arg, input, env, &mut |needle| match (input, &needle) {
(Value::String(s), Value::String(n)) => {
if n.is_empty() {
output(Value::Null);
} else if let Some(byte_pos) = s.rfind(n.as_str()) {
output(Value::Int(s[..byte_pos].chars().count() as i64));
} else {
output(Value::Null);
}
}
(Value::Array(arr), _) => {
let pos = arr.iter().rposition(|v| values_equal(v, &needle));
match pos {
Some(i) => output(Value::Int(i as i64)),
None => output(Value::Null),
}
}
_ => output(Value::Null),
});
}
}
"indices" | "_indices" => {
if let Some(arg) = args.first() {
eval(arg, input, env, &mut |needle| match (input, &needle) {
(Value::String(s), Value::String(n)) => {
let mut positions = Vec::new();
if !n.is_empty() {
let mut start = 0;
while let Some(byte_pos) = s[start..].find(n.as_str()) {
let abs_byte = start + byte_pos;
positions.push(Value::Int(s[..abs_byte].chars().count() as i64));
// Advance by 1 character to allow overlapping matches
let next_char_len =
s[abs_byte..].chars().next().map_or(1, |c| c.len_utf8());
start = abs_byte + next_char_len;
}
}
output(Value::Array(Arc::new(positions)));
}
(Value::Array(arr), Value::Array(needle_arr)) => {
// Subsequence search for array needles
let mut positions = Vec::new();
let nlen = needle_arr.len();
if nlen > 0 && nlen <= arr.len() {
for i in 0..=arr.len() - nlen {
let matches = arr[i..i + nlen]
.iter()
.zip(needle_arr.iter())
.all(|(a, b)| values_equal(a, b));
if matches {
positions.push(Value::Int(i as i64));
}
}
}
output(Value::Array(Arc::new(positions)));
}
(Value::Array(arr), _) => {
let positions: Vec<Value> = arr
.iter()
.enumerate()
.filter(|(_, v)| values_equal(v, &needle))
.map(|(i, _)| Value::Int(i as i64))
.collect();
output(Value::Array(Arc::new(positions)));
}
_ => output(Value::Array(Arc::new(Vec::new()))),
});
}
}
"explode" => {
if let Value::String(s) = input {
let codepoints: Vec<Value> = s.chars().map(|c| Value::Int(c as i64)).collect();
output(Value::Array(Arc::new(codepoints)));
} else {
set_error(format!(
"{} ({}) cannot be exploded",
input.type_name(),
input.short_desc()
));
}
}
"implode" => {
if let Value::Array(arr) = input {
let mut s = String::new();
for v in arr.iter() {
let cp = match v {
Value::Int(n) => *n,
Value::Double(f, _) if f.is_finite() => *f as i64,
_ => {
// jq format: "<type> (<value>) can't be imploded, ..."
// NaN/Infinity doubles display as "null" in jq
let desc = match v {
Value::Double(f, _) if f.is_nan() || f.is_infinite() => {
"null".to_string()
}
_ => v.short_desc(),
};
set_error(format!(
"{} ({}) can't be imploded, unicode codepoint needs to be numeric",
v.type_name(),
desc
));
return;
}
};
// Out-of-range or surrogate → replacement char U+FFFD
if !(0..=1114111).contains(&cp) || (55296..=57343).contains(&cp) {
s.push('\u{FFFD}');
} else if let Some(c) = char::from_u32(cp as u32) {
s.push(c);
} else {
s.push('\u{FFFD}');
}
}
output(Value::String(s));
} else {
set_error("implode input must be an array".to_string());
}
}
"tojson" => {
let mut buf = Vec::new();
crate::output::write_compact(&mut buf, input, false).unwrap();
output(Value::String(String::from_utf8(buf).unwrap_or_default()));
}
"fromjson" => {
if let Value::String(s) = input {
let trimmed = s.trim();
// Handle special numeric values that JSON doesn't normally support
match trimmed {
"NaN" | "nan" | "-NaN" | "-nan" => {
output(Value::Double(f64::NAN, None));
return;
}
"Infinity" | "infinity" | "inf" => {
output(Value::Double(f64::INFINITY, None));
return;
}
"-Infinity" | "-infinity" | "-inf" => {
output(Value::Double(f64::NEG_INFINITY, None));
return;
}
_ => {}
}
// Reject NaN/nan prefix followed by extra chars (e.g., "NaN1")
if (trimmed.starts_with("NaN")
|| trimmed.starts_with("nan")
|| trimmed.starts_with("-NaN")
|| trimmed.starts_with("-nan"))
&& trimmed != "NaN"
&& trimmed != "nan"
&& trimmed != "-NaN"
&& trimmed != "-nan"
{
set_error(format!(
"Invalid numeric literal at EOF at line 1, column {} (while parsing '{}')",
trimmed.len(),
safe_truncate(s, 40)
));
return;
}
let padded = crate::simdjson::pad_buffer(s.as_bytes());
match crate::simdjson::dom_parse_to_value(&padded, s.len()) {
Ok(val) => output(val),
Err(_e) => {
// Produce jq-compatible error message
if trimmed.contains('\'') {
// Single-quote detection — find position after closing single-quote
// Use char_indices for safe slicing on multi-byte boundaries
let first = trimmed
.char_indices()
.find(|(_, c)| *c == '\'')
.map(|(i, _)| i)
.unwrap_or(0);
let after_first =
if first < trimmed.len() && trimmed.is_char_boundary(first + 1) {
first + 1
} else {
// Advance past the multi-byte char
trimmed[first..]
.char_indices()
.nth(1)
.map(|(i, _)| first + i)
.unwrap_or(trimmed.len())
};
let col = trimmed[after_first..]
.find('\'')
.map(|p| after_first + p + 1 + 1) // +1 past close quote, +1 for 1-indexed
.unwrap_or(after_first);
set_error(format!(
"Invalid string literal; expected \", but got ' at line 1, column {} (while parsing '{}')",
col,
safe_truncate(s, 40)
));
} else {
set_error(format!(
"Invalid numeric literal at EOF at line 1, column {} (while parsing '{}')",
s.len(),
safe_truncate(s, 40)
));
}
}
}
} else {
set_error(format!(
"{} ({}) is not a string and cannot be parsed as JSON",
input.type_name(),
input.short_desc()
));
}
}
"utf8bytelength" => {
if let Value::String(s) = input {
output(Value::Int(s.len() as i64));
} else {
set_error(format!(
"{} ({}) only strings have UTF-8 byte length",
input.type_name(),
input.short_desc()
));
}
}
"ascii" => {
if let Value::String(s) = input
&& let Some(c) = s.chars().next()
{
output(Value::Int(c as i64));
}
}
"toboolean" => match input {
Value::Bool(_) => output(input.clone()),
Value::String(s) => match s.as_str() {
"true" => output(Value::Bool(true)),
"false" => output(Value::Bool(false)),
_ => {
set_error(format!(
"{} ({}) cannot be parsed as a boolean",
input.type_name(),
input.short_desc()
));
}
},
_ => {
set_error(format!(
"{} ({}) cannot be parsed as a boolean",
input.type_name(),
input.short_desc()
));
}
},
_ => {}
}
}