json.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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