File size: 36,528 Bytes
1851bae | 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 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 | use serde::Deserialize;
use crate::error::{JsonRepairError, Result};
pub struct JsonRepairParser {
chars: Vec<char>,
i: usize,
output: String,
}
impl JsonRepairParser {
pub fn new(text: String) -> Self {
Self { chars: text.chars().collect(), i: 0, output: String::new() }
}
pub fn parse<De: for<'de> Deserialize<'de>>(mut self) -> Result<De> {
self.parse_markdown_code_block(&["```", "[```", "{```"]);
let processed = self.parse_value()?;
if !processed {
return Err(JsonRepairError::UnexpectedEnd { position: self.chars.len() });
}
self.parse_markdown_code_block(&["```", "```]", "```}"]);
let processed_comma = self.parse_character(',');
if processed_comma {
self.parse_whitespace_and_skip_comments(true);
}
if self.is_start_of_value(self.current_char()) && self.ends_with_comma_or_newline() {
if !processed_comma {
self.output = self.insert_before_last_whitespace(",");
}
self.parse_newline_delimited_json()?;
} else if processed_comma {
self.output = self.strip_last_occurrence(",");
}
// repair redundant end quotes
while matches!(self.current_char(), Some('}' | ']')) {
self.i += 1;
self.parse_whitespace_and_skip_comments(true);
}
if self.i >= self.chars.len() {
return Ok(serde_json::from_str::<De>(&self.output)?);
}
Err(JsonRepairError::UnexpectedCharacter {
character: self.current_char().unwrap_or('\0'),
position: self.i,
})
}
fn current_char(&self) -> Option<char> {
self.chars.get(self.i).copied()
}
fn parse_value(&mut self) -> Result<bool> {
self.parse_whitespace_and_skip_comments(true);
let processed = self.parse_object()?
|| self.parse_array()?
|| self.parse_string(false, None)?
|| self.parse_number()?
|| self.parse_keywords()
|| self.parse_unquoted_string(false)?
|| self.parse_regex()?;
self.parse_whitespace_and_skip_comments(true);
Ok(processed)
}
fn parse_whitespace_and_skip_comments(&mut self, skip_newline: bool) -> bool {
let start = self.i;
self.parse_whitespace(skip_newline);
loop {
if self.parse_comment() {
self.parse_whitespace(skip_newline);
} else {
break;
}
}
self.i > start
}
fn parse_whitespace(&mut self, skip_newline: bool) -> bool {
let mut whitespace = String::new();
while let Some(ch) = self.current_char() {
if self.is_whitespace(ch, skip_newline) {
whitespace.push(ch);
self.i += 1;
} else if self.is_special_whitespace(ch) {
whitespace.push(' ');
self.i += 1;
} else {
break;
}
}
if !whitespace.is_empty() {
self.output.push_str(&whitespace);
true
} else {
false
}
}
fn parse_comment(&mut self) -> bool {
// block comment /* ... */
if self.current_char() == Some('/') && self.chars.get(self.i + 1) == Some(&'*') {
while self.i < self.chars.len() && !self.at_end_of_block_comment() {
self.i += 1;
}
self.i += 2;
return true;
}
// line comment // ...
if self.current_char() == Some('/') && self.chars.get(self.i + 1) == Some(&'/') {
while self.i < self.chars.len() && self.current_char() != Some('\n') {
self.i += 1;
}
return true;
}
false
}
fn parse_markdown_code_block(&mut self, blocks: &[&str]) -> bool {
if self.skip_markdown_code_block(blocks) {
if self.is_function_name_char_start(self.current_char()) {
while self.i < self.chars.len() && self.is_function_name_char(self.current_char()) {
self.i += 1;
}
}
self.parse_whitespace_and_skip_comments(true);
return true;
}
false
}
fn skip_markdown_code_block(&mut self, blocks: &[&str]) -> bool {
for block in blocks {
let block_chars: Vec<char> = block.chars().collect();
if self.i + block_chars.len() <= self.chars.len() {
let slice = &self.chars.get(self.i..self.i + block_chars.len());
if slice == &Some(block_chars.as_slice()) {
self.i += block_chars.len();
return true;
}
}
}
false
}
fn parse_character(&mut self, expected: char) -> bool {
if self.current_char() == Some(expected) {
self.output.push(expected);
self.i += 1;
true
} else {
false
}
}
fn skip_character(&mut self, expected: char) -> bool {
if self.current_char() == Some(expected) {
self.i += 1;
true
} else {
false
}
}
fn skip_ellipsis(&mut self) -> bool {
self.parse_whitespace_and_skip_comments(true);
if self.current_char() == Some('.')
&& self.chars.get(self.i + 1) == Some(&'.')
&& self.chars.get(self.i + 2) == Some(&'.')
{
self.i += 3;
self.parse_whitespace_and_skip_comments(true);
self.skip_character(',');
return true;
}
false
}
fn parse_object(&mut self) -> Result<bool> {
if self.current_char() != Some('{') {
return Ok(false);
}
self.output.push('{');
self.i += 1;
self.parse_whitespace_and_skip_comments(true);
// repair: skip leading comma like in {, message: "hi"}
if self.skip_character(',') {
self.parse_whitespace_and_skip_comments(true);
}
let mut initial = true;
while self.i < self.chars.len() && self.current_char() != Some('}') {
let _processed_comma = if !initial {
let comma = self.parse_character(',');
if !comma {
// repair missing comma
self.output = self.insert_before_last_whitespace(",");
}
self.parse_whitespace_and_skip_comments(true);
comma
} else {
initial = false;
true
};
self.skip_ellipsis();
let processed_key =
self.parse_string(false, None)? || self.parse_unquoted_string(true)?;
if !processed_key {
if matches!(self.current_char(), Some('}' | '{' | ']' | '[') | None) {
// repair trailing comma
self.output = self.strip_last_occurrence(",");
} else {
return Err(JsonRepairError::ObjectKeyExpected { position: self.i });
}
break;
}
self.parse_whitespace_and_skip_comments(true);
let processed_colon = self.parse_character(':');
let truncated_text = self.i >= self.chars.len();
if !processed_colon {
if self.is_start_of_value(self.current_char()) || truncated_text {
// repair missing colon
self.output = self.insert_before_last_whitespace(":");
} else {
return Err(JsonRepairError::ColonExpected { position: self.i });
}
}
let processed_value = self.parse_value()?;
if !processed_value {
if processed_colon || truncated_text {
// repair missing object value
self.output.push_str("null");
} else {
return Err(JsonRepairError::ColonExpected { position: self.i });
}
}
}
if self.current_char() == Some('}') {
self.output.push('}');
self.i += 1;
} else {
// repair missing end bracket
self.output = self.insert_before_last_whitespace("}");
}
Ok(true)
}
fn parse_array(&mut self) -> Result<bool> {
if self.current_char() != Some('[') {
return Ok(false);
}
self.output.push('[');
self.i += 1;
self.parse_whitespace_and_skip_comments(true);
// repair: skip leading comma like in [,1,2,3]
if self.skip_character(',') {
self.parse_whitespace_and_skip_comments(true);
}
let mut initial = true;
while self.i < self.chars.len() && self.current_char() != Some(']') {
if !initial {
let processed_comma = self.parse_character(',');
if !processed_comma {
// repair missing comma
self.output = self.insert_before_last_whitespace(",");
}
} else {
initial = false;
}
self.skip_ellipsis();
let processed_value = self.parse_value()?;
if !processed_value {
// repair trailing comma
self.output = self.strip_last_occurrence(",");
break;
}
}
if self.current_char() == Some(']') {
self.output.push(']');
self.i += 1;
} else {
// repair missing closing array bracket
self.output = self.insert_before_last_whitespace("]");
}
Ok(true)
}
fn parse_newline_delimited_json(&mut self) -> Result<()> {
let mut initial = true;
let mut processed_value = true;
while processed_value {
if !initial {
let processed_comma = self.parse_character(',');
if !processed_comma {
// repair: add missing comma
self.output = self.insert_before_last_whitespace(",");
}
} else {
initial = false;
}
processed_value = self.parse_value()?;
}
if !processed_value {
// repair: remove trailing comma
self.output = self.strip_last_occurrence(",");
}
// repair: wrap the output inside array brackets
self.output = format!("[\n{}\n]", self.output);
Ok(())
}
fn parse_string(
&mut self,
stop_at_delimiter: bool,
stop_at_index: Option<usize>,
) -> Result<bool> {
let skip_escape_chars = self.current_char() == Some('\\');
if skip_escape_chars {
// repair: remove the first escape character
self.i += 1;
}
if !self.is_quote(self.current_char()) {
return Ok(false);
}
let quote_char = self.current_char().unwrap();
let is_end_quote = self.get_end_quote_matcher(quote_char);
let i_before = self.i;
let o_before = self.output.len();
let mut str_content = String::from("\"");
self.i += 1;
loop {
if self.i >= self.chars.len() {
// end of text, missing end quote
let i_prev = self.prev_non_whitespace_index(self.i - 1);
if !stop_at_delimiter && self.is_delimiter(self.chars.get(i_prev).copied()) {
// retry parsing the string, stopping at the first next delimiter
self.i = i_before;
self.output.truncate(o_before);
return self.parse_string(true, None);
}
// repair missing quote
str_content = self.insert_before_last_whitespace_str(&str_content, "\"");
self.output.push_str(&str_content);
return Ok(true);
}
if let Some(stop_idx) = stop_at_index
&& self.i == stop_idx
{
str_content = self.insert_before_last_whitespace_str(&str_content, "\"");
self.output.push_str(&str_content);
return Ok(true);
}
let ch = self.current_char().unwrap();
if is_end_quote(ch) {
// end quote - verify if it's legit
let i_quote = self.i;
let o_quote = str_content.len();
str_content.push('"');
self.i += 1;
self.output.push_str(&str_content);
self.parse_whitespace_and_skip_comments(false);
if stop_at_delimiter
|| self.i >= self.chars.len()
|| self.is_delimiter(self.current_char())
|| self.is_quote(self.current_char())
|| self.is_digit(self.current_char())
{
// legitimate end quote
self.parse_concatenated_string()?;
return Ok(true);
}
let i_prev_char = self.prev_non_whitespace_index(i_quote - 1);
let prev_char = self.chars.get(i_prev_char).copied();
if prev_char == Some(',') {
// comma followed by quote - missing end quote before comma
self.i = i_before;
self.output.truncate(o_before);
return self.parse_string(false, Some(i_prev_char));
}
if self.is_delimiter(prev_char) {
// not the right end quote - retry with stop at delimiter
self.i = i_before;
self.output.truncate(o_before);
return self.parse_string(true, None);
}
// revert and continue - unescaped quote
self.output.truncate(o_before);
self.i = i_quote + 1;
let prefix = str_content.get(..o_quote).unwrap_or("");
str_content = format!("{}\\\"", prefix);
} else if stop_at_delimiter && self.is_unquoted_string_delimiter(ch) {
// stop at delimiter mode
// test for URL like "https://..."
let url_candidate = if self.chars.get(self.i - 1) == Some(&':')
&& i_before + 1 < self.chars.len()
&& self.i + 2 <= self.chars.len()
{
self.chars.get(i_before + 1..self.i + 2)
} else {
None
};
if url_candidate.is_some_and(|s| self.is_url_start(s)) {
while self.i < self.chars.len()
&& self.is_url_char(self.current_char().unwrap())
{
str_content.push(self.current_char().unwrap());
self.i += 1;
}
}
// repair missing quote
str_content = self.insert_before_last_whitespace_str(&str_content, "\"");
self.output.push_str(&str_content);
self.parse_concatenated_string()?;
return Ok(true);
} else if ch == '\\' {
// handle escaped content
if let Some(next_ch) = self.chars.get(self.i + 1) {
match next_ch {
'"' | '\\' | '/' | 'b' | 'f' | 'n' | 'r' | 't' => {
let escaped: String = self
.chars
.get(self.i..self.i + 2)
.map(|s| s.iter().collect())
.unwrap_or_default();
str_content.push_str(&escaped);
self.i += 2;
}
',' if skip_escape_chars => {
// Special case: escaped comma in escaped string should be treated as
// delimiter This creates a new array element
str_content =
self.insert_before_last_whitespace_str(&str_content, "\"");
self.output.push_str(&str_content);
self.output.push(',');
self.i += 2; // skip \,
self.parse_whitespace_and_skip_comments(true);
return Ok(true);
}
'u' => {
let mut j = 2;
while j < 6 && self.i + j < self.chars.len() {
let hex_ch = self.chars.get(self.i + j).copied().unwrap_or('\0');
if !self.is_hex(hex_ch) {
break;
}
j += 1;
}
if j == 6 {
let unicode_seq: String = self
.chars
.get(self.i..self.i + 6)
.map(|s| s.iter().collect())
.unwrap_or_default();
str_content.push_str(&unicode_seq);
self.i += 6;
} else if self.i + j >= self.chars.len() {
// repair invalid unicode at end
self.i = self.chars.len();
} else {
// SAFETY: j <= 6 and self.i + j < self.chars.len() (else branch
// above handles >=)
let invalid_chars: String = self
.chars
.get(self.i..self.i + j.min(6))
.map(|s| s.iter().collect())
.unwrap_or_default();
return Err(JsonRepairError::InvalidUnicodeCharacter {
chars: invalid_chars,
position: self.i,
});
}
}
_ => {
// repair invalid escape character
str_content.push(*next_ch);
self.i += 2;
}
}
} else {
self.i += 1;
}
} else {
// handle regular characters
if ch == '"' && self.chars.get(self.i - 1) != Some(&'\\') {
// repair unescaped double quote
str_content.push_str("\\\"");
self.i += 1;
} else if self.is_control_character(ch) {
// unescaped control character
str_content.push_str(&self.get_control_character_escape(ch));
self.i += 1;
} else {
if !self.is_valid_string_character(ch) {
return Err(JsonRepairError::InvalidCharacter {
character: ch,
position: self.i,
});
}
str_content.push(ch);
self.i += 1;
}
}
if skip_escape_chars {
// repair: skip escape character
if self.current_char() == Some('\\') {
self.i += 1;
}
}
}
}
fn parse_concatenated_string(&mut self) -> Result<bool> {
let mut processed = false;
self.parse_whitespace_and_skip_comments(true);
while self.current_char() == Some('+') {
processed = true;
self.i += 1;
self.parse_whitespace_and_skip_comments(true);
// repair: remove the end quote of the first string
self.output = self.strip_last_occurrence_with_remaining("\"", true);
let start = self.output.len();
let parsed_str = self.parse_string(false, None)?;
if parsed_str {
// repair: remove the start quote of the second string
self.output = self.remove_at_index(start, 1);
} else {
// repair: remove the + because it's not followed by a string
self.output = self.insert_before_last_whitespace("\"");
}
}
Ok(processed)
}
fn parse_number(&mut self) -> Result<bool> {
let start = self.i;
if self.current_char() == Some('-') {
self.i += 1;
if self.at_end_of_number() {
self.repair_number_ending_with_numeric_symbol(start);
return Ok(true);
}
if !self.is_digit(self.current_char()) {
self.i = start;
return Ok(false);
}
}
while self.is_digit(self.current_char()) {
self.i += 1;
}
if self.current_char() == Some('.') {
self.i += 1;
if self.at_end_of_number() {
self.repair_number_ending_with_numeric_symbol(start);
return Ok(true);
}
if !self.is_digit(self.current_char()) {
self.i = start;
return Ok(false);
}
while self.is_digit(self.current_char()) {
self.i += 1;
}
}
if matches!(self.current_char(), Some('e' | 'E')) {
self.i += 1;
if matches!(self.current_char(), Some('-' | '+')) {
self.i += 1;
}
if self.at_end_of_number() {
self.repair_number_ending_with_numeric_symbol(start);
return Ok(true);
}
if !self.is_digit(self.current_char()) {
self.i = start;
return Ok(false);
}
while self.is_digit(self.current_char()) {
self.i += 1;
}
}
if !self.at_end_of_number() {
self.i = start;
return Ok(false);
}
if self.i > start {
let num: String = self
.chars
.get(start..self.i)
.map(|s| s.iter().collect())
.unwrap_or_default();
let has_invalid_leading_zero =
num.len() > 1 && num.starts_with('0') && self.is_digit(num.chars().nth(1));
if has_invalid_leading_zero {
self.output.push_str(&serde_json::to_string(&num).unwrap());
} else {
self.output.push_str(&num);
}
return Ok(true);
}
Ok(false)
}
fn parse_keywords(&mut self) -> bool {
self.parse_keyword("true", "true")
|| self.parse_keyword("false", "false")
|| self.parse_keyword("null", "null")
|| self.parse_keyword("True", "true")
|| self.parse_keyword("False", "false")
|| self.parse_keyword("None", "null")
}
fn parse_keyword(&mut self, name: &str, value: &str) -> bool {
let name_chars: Vec<char> = name.chars().collect();
if self.i + name_chars.len() <= self.chars.len() {
let slice = self.chars.get(self.i..self.i + name_chars.len());
if slice == Some(name_chars.as_slice()) {
self.output.push_str(value);
self.i += name_chars.len();
return true;
}
}
false
}
fn parse_unquoted_string(&mut self, is_key: bool) -> Result<bool> {
let start = self.i;
if self.is_function_name_char_start(self.current_char()) {
while self.i < self.chars.len() && self.is_function_name_char(self.current_char()) {
self.i += 1;
}
let mut j = self.i;
while j < self.chars.len() {
let ch = self.chars.get(j).copied().unwrap_or('\0');
if !self.is_whitespace(ch, true) {
break;
}
j += 1;
}
if j < self.chars.len() {
let ch_at_j = self.chars.get(j).copied().unwrap_or('\0');
if ch_at_j == '(' {
// function call
self.i = j + 1;
self.parse_value()?;
if self.current_char() == Some(')') {
self.i += 1;
if self.current_char() == Some(';') {
self.i += 1;
}
}
return Ok(true);
}
}
}
// reset for URL/unquoted string parsing
self.i = start;
while self.i < self.chars.len() {
let ch = self.current_char().unwrap();
if self.is_unquoted_string_delimiter(ch)
|| self.is_quote(Some(ch))
|| (is_key && ch == ':')
{
break;
}
self.i += 1;
}
// test for URL
if self.i > 0
&& self.chars.get(self.i - 1) == Some(&':')
&& start < self.chars.len()
&& self.i + 2 <= self.chars.len()
&& self.is_url_start(self.chars.get(start..self.i + 2).unwrap_or(&[]))
{
while self.i < self.chars.len() && self.is_url_char(self.current_char().unwrap()) {
self.i += 1;
}
}
if self.i > start {
// remove trailing whitespace
while self.i > start
&& self.is_whitespace(self.chars.get(self.i - 1).copied().unwrap_or('\0'), true)
{
self.i -= 1;
}
let symbol: String = self
.chars
.get(start..self.i)
.map(|s| s.iter().collect())
.unwrap_or_default();
if symbol == "undefined" {
self.output.push_str("null");
} else if is_key {
// Keys must always be strings in JSON
self.output
.push_str(&serde_json::to_string(&symbol).unwrap());
} else {
// Try to parse as number first, if that fails, treat as string
if symbol.parse::<f64>().is_ok() {
// It's a valid number, output as-is
self.output.push_str(&symbol);
} else {
// It's a string, so quote it using serde_json
self.output
.push_str(&serde_json::to_string(&symbol).unwrap());
}
}
if self.current_char() == Some('"') {
// missing start quote, skip end quote
self.i += 1;
}
return Ok(true);
}
Ok(false)
}
fn parse_regex(&mut self) -> Result<bool> {
if self.current_char() == Some('/') {
let start = self.i;
self.i += 1;
while self.i < self.chars.len()
&& (self.current_char() != Some('/')
|| (self.i > 0 && self.chars.get(self.i - 1) == Some(&'\\')))
{
self.i += 1;
}
// Only increment if we found the closing slash
if self.i < self.chars.len() {
self.i += 1;
}
let regex: String = self
.chars
.get(start..self.i)
.map(|s| s.iter().collect())
.unwrap_or_default();
self.output
.push_str(&serde_json::to_string(®ex).unwrap());
return Ok(true);
}
Ok(false)
}
// Helper methods
fn is_whitespace(&self, ch: char, skip_newline: bool) -> bool {
if skip_newline {
ch == ' ' || ch == '\n' || ch == '\t' || ch == '\r'
} else {
ch == ' ' || ch == '\t' || ch == '\r'
}
}
fn is_special_whitespace(&self, ch: char) -> bool {
matches!(ch as u32, 0xa0 | 0x2000..=0x200a | 0x202f | 0x205f | 0x3000)
}
fn is_quote(&self, ch: Option<char>) -> bool {
matches!(
ch,
Some('"' | '\'' | '`' | '\u{2018}' | '\u{2019}' | '\u{201c}' | '\u{201d}' | '\u{00b4}')
)
}
fn is_digit(&self, ch: Option<char>) -> bool {
matches!(ch, Some('0'..='9'))
}
fn is_hex(&self, ch: char) -> bool {
ch.is_ascii_hexdigit()
}
fn is_function_name_char_start(&self, ch: Option<char>) -> bool {
matches!(ch, Some('a'..='z' | 'A'..='Z' | '_' | '$'))
}
fn is_function_name_char(&self, ch: Option<char>) -> bool {
matches!(ch, Some('a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '$'))
}
fn is_delimiter(&self, ch: Option<char>) -> bool {
matches!(
ch,
Some(',' | ':' | '[' | ']' | '/' | '{' | '}' | '(' | ')' | '\n' | '+')
)
}
fn is_unquoted_string_delimiter(&self, ch: char) -> bool {
matches!(ch, ',' | '[' | ']' | '/' | '{' | '}' | '\n' | '+')
}
fn is_start_of_value(&self, ch: Option<char>) -> bool {
self.is_quote(ch)
|| matches!(
ch,
Some('[' | '{' | 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-')
)
}
fn is_control_character(&self, ch: char) -> bool {
matches!(ch, '\n' | '\r' | '\t' | '\u{08}' | '\u{0c}')
}
fn is_valid_string_character(&self, ch: char) -> bool {
ch >= '\u{0020}'
}
fn is_url_start(&self, chars: &[char]) -> bool {
if chars.len() < 7 {
return false;
}
let s: String = chars.iter().collect();
s.starts_with("http://")
|| s.starts_with("https://")
|| s.starts_with("ftp://")
|| s.starts_with("mailto:")
|| s.starts_with("file://")
|| s.starts_with("data:")
|| s.starts_with("irc://")
}
fn is_url_char(&self, ch: char) -> bool {
ch.is_ascii_alphanumeric()
|| matches!(
ch,
'-' | '.'
| '_'
| '~'
| ':'
| '/'
| '?'
| '#'
| '@'
| '!'
| '$'
| '&'
| '\''
| '('
| ')'
| '*'
| '+'
| ';'
| '='
)
}
fn get_end_quote_matcher(&self, quote_char: char) -> fn(char) -> bool {
match quote_char {
'"' => |ch| ch == '"',
'\'' => |ch| ch == '\'',
'\u{2018}' => |ch| matches!(ch, '\u{2018}' | '\u{2019}'),
'\u{2019}' => |ch| matches!(ch, '\u{2018}' | '\u{2019}'),
'\u{201c}' => |ch| matches!(ch, '\u{201c}' | '\u{201d}'),
'\u{201d}' => |ch| matches!(ch, '\u{201c}' | '\u{201d}'),
'`' => |ch| matches!(ch, '`' | '\u{00b4}' | '\''),
'\u{00b4}' => |ch| matches!(ch, '`' | '\u{00b4}' | '\''),
_ => |ch| ch == '"',
}
}
fn get_control_character_escape(&self, ch: char) -> String {
match ch {
'\u{08}' => "\\b".to_string(),
'\u{0c}' => "\\f".to_string(),
'\n' => "\\n".to_string(),
'\r' => "\\r".to_string(),
'\t' => "\\t".to_string(),
_ => ch.to_string(),
}
}
fn at_end_of_block_comment(&self) -> bool {
self.current_char() == Some('*') && self.chars.get(self.i + 1) == Some(&'/')
}
fn prev_non_whitespace_index(&self, start: usize) -> usize {
let mut prev = start;
while prev > 0
&& prev < self.chars.len()
&& self.is_whitespace(self.chars.get(prev).copied().unwrap_or('\0'), true)
{
prev = prev.saturating_sub(1);
}
prev
}
fn at_end_of_number(&self) -> bool {
self.i >= self.chars.len()
|| self.is_delimiter(self.current_char())
|| self.is_whitespace(self.current_char().unwrap_or('\0'), true)
}
fn repair_number_ending_with_numeric_symbol(&mut self, start: usize) {
let num: String = self
.chars
.get(start..self.i)
.map(|s| s.iter().collect())
.unwrap_or_default();
self.output.push_str(&format!("{num}0"));
}
fn ends_with_comma_or_newline(&self) -> bool {
let regex = regex::Regex::new(r"[,\n][ \t\r]*$").unwrap();
regex.is_match(&self.output)
}
fn strip_last_occurrence(&self, text_to_strip: &str) -> String {
self.strip_last_occurrence_with_remaining(text_to_strip, false)
}
fn strip_last_occurrence_with_remaining(
&self,
text_to_strip: &str,
strip_remaining: bool,
) -> String {
if let Some(index) = self.output.rfind(text_to_strip) {
let mut result = self.output.get(..index).unwrap_or("").to_string();
if !strip_remaining {
result.push_str(self.output.get(index + text_to_strip.len()..).unwrap_or(""));
}
result
} else {
self.output.clone()
}
}
fn insert_before_last_whitespace(&self, text_to_insert: &str) -> String {
let chars: Vec<char> = self.output.chars().collect();
let mut index = chars.len();
if index == 0 || !self.is_whitespace(chars.get(index - 1).copied().unwrap_or('\0'), true) {
return format!("{}{}", self.output, text_to_insert);
}
while index > 0 && self.is_whitespace(chars.get(index - 1).copied().unwrap_or('\0'), true) {
index -= 1;
}
// Convert the char-based index back to a byte offset for string slicing.
let byte_index = self
.output
.char_indices()
.nth(index)
.map_or(self.output.len(), |(i, _)| i);
format!(
"{}{}{}",
self.output.get(..byte_index).unwrap_or(""),
text_to_insert,
self.output.get(byte_index..).unwrap_or("")
)
}
fn insert_before_last_whitespace_str(&self, text: &str, text_to_insert: &str) -> String {
let chars: Vec<char> = text.chars().collect();
let mut index = chars.len();
if index == 0 || !self.is_whitespace(chars.get(index - 1).copied().unwrap_or('\0'), true) {
return format!("{text}{text_to_insert}");
}
while index > 0 && self.is_whitespace(chars.get(index - 1).copied().unwrap_or('\0'), true) {
index -= 1;
}
// Convert the char-based index back to a byte offset for string slicing.
let byte_index = text
.char_indices()
.nth(index)
.map_or(text.len(), |(i, _)| i);
format!(
"{}{}{}",
text.get(..byte_index).unwrap_or(""),
text_to_insert,
text.get(byte_index..).unwrap_or("")
)
}
fn remove_at_index(&self, start: usize, count: usize) -> String {
let chars: Vec<char> = self.output.chars().collect();
let mut result = String::new();
for (i, ch) in chars.iter().enumerate() {
if i < start || i >= start + count {
result.push(*ch);
}
}
result
}
}
pub fn json_repair<De: for<'de> Deserialize<'de>>(text: &str) -> Result<De> {
let parser = JsonRepairParser::new(text.to_string());
parser.parse()
}
|