• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright JS Foundation and other contributors, http://js.foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16LICENSE = """/* Copyright JS Foundation and other contributors, http://js.foundation
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 *     http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS
26 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
29 */"""
30
31
32def format_code(code, indent, digit_number=4):
33    def regroup(list_to_group, num):
34        return [list_to_group[i:i+num] for i in range(0, len(list_to_group), num)]
35
36    def hex_format(char, digit_number):
37        if isinstance(char, str):
38            char = ord(char)
39
40        return ("0x{:0%sx}" % digit_number).format(char)
41
42    lines = []
43
44    nums_per_line = 10
45    width = nums_per_line * (digit_number + 4)
46    # convert all characters to hex format
47    converted_code = [hex_format(char, digit_number) for char in code]
48    # 10 hex number per line
49    for line in regroup(", ".join(converted_code), width):
50        lines.append(('  ' * indent) + line.strip())
51    return "\n".join(lines)
52