MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / write_atom_literal

Function write_atom_literal

javascript/private/gen_file.py:28–74  ·  view source on GitHub ↗
(out, name, contents, lang, utf8)

Source from the content-addressed store, hash-verified

26
27
28def write_atom_literal(out, name, contents, lang, utf8):
29 # Escape the contents of the file so it can be stored as a literal.
30 contents = contents.replace("\\", "\\\\")
31 contents = contents.replace("\f", "\\f")
32 contents = contents.replace("\n", "\\n")
33 contents = contents.replace("\r", "\\r")
34 contents = contents.replace("\t", "\\t")
35 contents = contents.replace('"', '\\"')
36
37 if "cc" == lang or "hh" == lang:
38 if utf8:
39 line_format = ' "{}",\n'
40 else:
41 line_format = ' L"{}",\n'
42 elif "java" == lang:
43 line_format = ' .append("{}")\n'
44 else:
45 raise RuntimeError(f"Unknown language: {lang} ")
46
47 name = get_atom_name(name)
48
49 if "cc" == lang or "hh" == lang:
50 char_type = "char" if utf8 else "wchar_t"
51 out.write(f"const {char_type}* const {name}[] = {{\n")
52 elif "java" == lang:
53 out.write(f" {name}(new StringBuilder()\n")
54 else:
55 raise RuntimeError(f"Unknown language: {lang} ")
56
57 # Make the header file play nicely in a terminal: limit lines to 80
58 # characters, but make sure we don't cut off a line in the middle
59 # of an escape sequence.
60 while len(contents) > 70:
61 diff = 70
62 while contents[diff - 1] == "\\":
63 diff = diff - 1
64 line = contents[0:diff]
65 contents = contents[diff:]
66
67 out.write(line_format.format(line))
68 if len(contents) > 0:
69 out.write(line_format.format(contents))
70
71 if "cc" == lang or "hh" == lang:
72 out.write(" NULL\n};\n\n")
73 elif "java" == lang:
74 out.write(" .toString()),\n")
75
76
77def generate_header(file_name, out, js_map, just_declare, utf8, note):

Callers 3

generate_headerFunction · 0.85
generate_cc_sourceFunction · 0.85
generate_java_sourceFunction · 0.85

Calls 3

get_atom_nameFunction · 0.85
writeMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected