Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

json.rs 1.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. /// Some of our JSON objects have \u0000, which PostgreSQL doesn't like.
  2. /// Clean those out while we import.
  3. /// This uses a reusable buffer to reduce allocations.
  4. ///
  5. /// ```
  6. /// use bookdata::cleaning::clean_json;
  7. /// let mut buf = String::new();
  8. /// clean_json("some bad text\\u0000 continued", &mut buf);
  9. /// assert_eq!(buf, "some bad text continued");
  10. /// ```
  11. pub fn clean_json(json: &str, buf: &mut String) {
  12. let bad = "\\u0000";
  13. buf.clear();
  14. let mut cur = json;
  15. loop {
  16. match cur.find(bad) {
  17. None => {
  18. buf.push_str(cur);
  19. break
  20. },
  21. Some(i) => {
  22. buf.push_str(&cur[0..i]);
  23. cur = &cur[(i + bad.len())..]
  24. }
  25. }
  26. }
  27. }
  28. #[test]
  29. fn clean_empty_is_empty() {
  30. let mut buf = String::new();
  31. clean_json("", &mut buf);
  32. assert_eq!(buf, "");
  33. }
  34. #[test]
  35. fn clean_only_is_empty() {
  36. let mut buf = String::new();
  37. clean_json("\\u0000", &mut buf);
  38. assert_eq!(buf, "");
  39. }
  40. #[test]
  41. fn clean_trail() {
  42. let mut buf = String::new();
  43. clean_json("bob\\u0000", &mut buf);
  44. assert_eq!(buf, "bob");
  45. }
  46. #[test]
  47. fn clean_lead() {
  48. let mut buf = String::new();
  49. clean_json("\\u0000wombat", &mut buf);
  50. assert_eq!(buf, "wombat");
  51. }
  52. #[test]
  53. fn clean_middle() {
  54. let mut buf = String::new();
  55. clean_json("heffalump\\u0000wumpus", &mut buf);
  56. assert_eq!(buf, "heffalumpwumpus");
  57. }
  58. #[test]
  59. fn clean_multi() {
  60. let mut buf = String::new();
  61. clean_json("bob\\u0000dylan\\u0000fish", &mut buf);
  62. assert_eq!(buf, "bobdylanfish");
  63. }
  64. #[test]
  65. fn clean_reuse_buffer() {
  66. let mut buf = String::new();
  67. clean_json("bob\\u0000dylan\\u0000fish", &mut buf);
  68. assert_eq!(buf, "bobdylanfish");
  69. clean_json("pizza fish", &mut buf);
  70. assert_eq!(buf, "pizza fish");
  71. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...