# -*- coding: utf-8 -*-"""Strip ``// comment`` style single-line comments from JSON5 strings."""def_strip_line_comment(line:str)->str:"""Remove a trailing ``//`` comment from a single line. Scans character by character so that ``//`` inside a string literal is left untouched. Handles ``\\"`` escaped quotes correctly. """in_string=Falsei=0whilei<len(line):ch=line[i]ifin_string:ifch=="\\":i+=2# skip escaped charactercontinueifch=='"':in_string=Falseelse:ifch=='"':in_string=Trueelifch=="/"andi+1<len(line)andline[i+1]=="/":returnline[:i].rstrip()i+=1returnline
[docs]defstrip_comments(string:str)->str:"""Strip ``//`` single-line comments from a JSON5 string. :param string: JSON5 text that may contain ``//`` comments. :return: The string with all ``//`` comments removed. """lines=string.splitlines()return"\n".join(_strip_line_comment(line)forlineinlines)