[libcamera-devel,09/10] utils: raspberrypi: ctt: json_pretty_print: Avoid spaces at end of lines

Message ID 20200703001422.24324-10-laurent.pinchart@ideasonboard.com
State Accepted
Headers show
Series
  • utils: raspberrypi: ctt: Improve JSON pretty printer
Related show

Commit Message

Laurent Pinchart July 3, 2020, 12:14 a.m. UTC
Avoid outputting spaces at end of lines by recording the need for a
space and outputting it before the next character only if not a newline.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 utils/raspberrypi/ctt/ctt_pretty_print_json.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Patch

diff --git a/utils/raspberrypi/ctt/ctt_pretty_print_json.py b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
index f7fdb65116bc..2ca307a0af11 100644
--- a/utils/raspberrypi/ctt/ctt_pretty_print_json.py
+++ b/utils/raspberrypi/ctt/ctt_pretty_print_json.py
@@ -18,6 +18,7 @@  class JSONPrettyPrinter(object):
             "arraycount": [],
             "skipnewline": True,
             "need_indent": False,
+            "need_space": False,
         }
 
         self.fout = fout
@@ -26,12 +27,16 @@  class JSONPrettyPrinter(object):
         if not self.state["skipnewline"]:
             self.fout.write('\n')
             self.state["need_indent"] = True
+            self.state["need_space"] = False
         self.state["skipnewline"] = True
 
     def write(self, c):
         if self.state["need_indent"]:
             self.fout.write(' ' * self.state["indent"] * 4)
             self.state["need_indent"] = False
+        if self.state["need_space"]:
+            self.fout.write(' ')
+            self.state["need_space"] = False
         self.fout.write(c)
         self.state["skipnewline"] = False
 
@@ -60,11 +65,10 @@  class JSONPrettyPrinter(object):
             self.write(c)
         elif c == ':':
             self.write(c)
-            self.write(' ')
+            self.state["need_space"] = True
         elif c == ',':
             if not self.state["inarray"][0]:
                 self.write(c)
-                self.write(' ')
                 self.newline()
             else:
                 self.write(c)
@@ -73,7 +77,7 @@  class JSONPrettyPrinter(object):
                     self.state["arraycount"][0] = 0
                     self.newline()
                 else:
-                    self.write(' ')
+                    self.state["need_space"] = True
         elif c.isspace():
             pass
         else: