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

Function write_atom_literal

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

Source from the content-addressed store, hash-verified

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

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