Skip to content

Commit

Permalink
use same formatter as ada
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 3, 2024
1 parent bb9807b commit 0fa93be
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
BasedOnStyle: Google
SortIncludes: false
SortIncludes: Never
37 changes: 20 additions & 17 deletions .github/workflows/lint_and_format_check.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
name: Lint and Format Check (Ubuntu Latest)
name: Lint and format

on:
push:
branches:
- "*"
pull_request:
push:
branches:
- "*"
- main

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
ubuntu-build:
lint-and-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install clang-format
run: |
sudo apt update && sudo apt install clang-format-15 -y
sudo ln -sf /usr/bin/clang-format-15 /usr/bin/clang-format
- name: Build with Lint and Format Check
run: |
cmake -B build && cmake --build build
env:
CXX: clang++-14
LINT_AND_FORMAT_CHECK: true
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

- name: Run clang-format
uses: jidicula/clang-format-action@c74383674bf5f7c69f60ce562019c1c94bc1421a # v4.13.0
with:
clang-format-version: '17'
fallback-style: 'Google'

- uses: chartboost/ruff-action@e18ae971ccee1b2d7bbef113930f00c670b78da4 # v1.0.0
name: Lint with Ruff
with:
version: 0.6.0
97 changes: 57 additions & 40 deletions scripts/idna_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,124 @@
import re
import textwrap
import sys

if sys.version_info[0] < 3:
print('You need to run this with Python 3')
print("You need to run this with Python 3")
sys.exit(1)

indent = ' ' * 4
indent = " " * 4


def fill(text):
tmp = textwrap.fill(text)
return textwrap.indent(tmp, indent)


def filltab(text):
tmp = textwrap.fill(text, width=120)
return textwrap.indent(tmp, '\t')
return textwrap.indent(tmp, "\t")


def cpp_array_initializer(arr):
return '{%s}' % (', '.join(map(str, arr)))
return "{%s}" % (", ".join(map(str, arr)))


def multiline_cpp_array_initializer(arr):
alllines = '{\n'
current_line = '\t'
alllines = "{\n"
current_line = "\t"
for i in arr:
if(len(current_line) > 80):
if(current_line[-1] == ' '):
if len(current_line) > 80:
if current_line[-1] == " ":
current_line = current_line[:-1]
alllines += current_line + '\n'
current_line = '\t'
alllines += current_line + "\n"
current_line = "\t"
current_line += str(i)
if(i != len(arr)-1): current_line += ', '
if(len(current_line) > 0):
if(current_line[-1] == ' '):
current_line = current_line[:-1]
alllines += current_line + '\n'
alllines += '\n}'
if i != len(arr) - 1:
current_line += ", "
if len(current_line) > 0:
if current_line[-1] == " ":
current_line = current_line[:-1]
alllines += current_line + "\n"
alllines += "\n}"
return alllines


def compose2(f, g):
return lambda x: f(g(x))


def cpp_arrayarray_initializer(arr):
alllines = '{'
for i in range(0,len(arr), 4):
alllines += '\n\t' + ', '.join(map(cpp_array_initializer,arr[i:i+4]))
if(i + 4 < len(arr)): alllines += ','
alllines += '\n}'
alllines = "{"
for i in range(0, len(arr), 4):
alllines += "\n\t" + ", ".join(map(cpp_array_initializer, arr[i : i + 4]))
if i + 4 < len(arr):
alllines += ","
alllines += "\n}"
return alllines


url = "https://www.unicode.org/Public/idna/15.0.0/IdnaMappingTable.txt"
filename = "IdnaMappingTable.txt"


def get_table():
if(not os.path.exists(filename)):
if not os.path.exists(filename):
tablefile = requests.get(url)
with open(filename, 'wb') as file:
with open(filename, "wb") as file:
file.write(tablefile.content)
with open(filename, 'r') as file:
return file.read()
with open(filename, "r") as file:
return file.read()


def get_version(table_data):
return re.search("# Version: (.*)", table_data).group(1)


def find_slice(seq, subseq):
n = len(seq)
m = len(subseq)
for i in range(n - m + 1):
if seq[i:i + m] == subseq:
if seq[i : i + m] == subseq:
return i
return -1


def values(words):
answer = []
for w in words:
w2 = w[1]
if(w[1] == 3):
if w[1] == 3:
w2 += (w[2][0] << 8) + (w[2][1] << 24)
answer.append([w[0][0], w2])
return answer

UseSTD3ASCIIRules=False

UseSTD3ASCIIRules = False
if __name__ == "__main__":
table_data = get_table()
print("// IDNA ", get_version(get_table()))
words = []
long_mapped = []
previous_code = -1
mapping = {"ignored":0,"valid":1,"disallowed":2,"mapped":3}
mapping = {"ignored": 0, "valid": 1, "disallowed": 2, "mapped": 3}
for line in table_data.split("\n"):
if(line.startswith("#") or ";" not in line):
if line.startswith("#") or ";" not in line:
continue
line = line[0:line.index("#")]
line = line[0 : line.index("#")]
line = line.split(";")
line = [x.strip() for x in line if x.strip() != ""]
if(not UseSTD3ASCIIRules and line[1].startswith("disallowed_STD3_")): line[1] = line[1][16:]
if(line[1] == "deviation"): line[1] = "valid" # IDNA2008, see https://www.unicode.org/faq/idn.html
codepoints = [int(x,16) for x in line[0].split("..")]
if not UseSTD3ASCIIRules and line[1].startswith("disallowed_STD3_"):
line[1] = line[1][16:]
if line[1] == "deviation":
line[1] = "valid" # IDNA2008, see https://www.unicode.org/faq/idn.html
codepoints = [int(x, 16) for x in line[0].split("..")]
code = mapping[line[1]]
mapped = []
if((code != 3) and (code == previous_code)):
continue # nothing to do
if (code != 3) and (code == previous_code):
continue # nothing to do
previous_code = code
if(code == 3):
mapped = [int(x,16) for x in line[2].split(" ")]
if code == 3:
mapped = [int(x, 16) for x in line[2].split(" ")]
already = find_slice(long_mapped, mapped)
if already != -1:
long_index = already
Expand All @@ -122,9 +139,9 @@ def values(words):
namespace ada::idna {
""")
print("const uint32_t mappings["+str(len(long_mapped))+"] = ")
print("const uint32_t mappings[" + str(len(long_mapped)) + "] = ")
print(multiline_cpp_array_initializer(long_mapped), end=";\n")
print("const uint32_t table["+str(len(words))+"][2] = ")
print("const uint32_t table[" + str(len(words)) + "][2] = ")
print(cpp_arrayarray_initializer(values(words)), end=";\n")
print("""
Expand Down
101 changes: 0 additions & 101 deletions scripts/lint_and_format.py

This file was deleted.

23 changes: 23 additions & 0 deletions scripts/run-clangcldocker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -e
COMMAND=$*
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
MAINSOURCE=$SCRIPTPATH/..
ALL_ADA_FILES=$(cd $MAINSOURCE && git ls-tree --full-tree --name-only -r HEAD | grep -e ".*\.\(c\|h\|cc\|cpp\|hh\)\$")

if clang-format-17 --version 2>/dev/null | grep -qF 'version 17.'; then
cd $MAINSOURCE; clang-format-17 --style=file --verbose -i "$@" $ALL_ADA_FILES
exit 0
elif clang-format --version 2>/dev/null | grep -qF 'version 17.'; then
cd $MAINSOURCE; clang-format --style=file --verbose -i "$@" $ALL_ADA_FILES
exit 0
fi
echo "Trying to use docker"
command -v docker >/dev/null 2>&1 || { echo >&2 "Please install docker. E.g., go to https://www.docker.com/products/docker-desktop Type 'docker' to diagnose the problem."; exit 1; }
docker info >/dev/null 2>&1 || { echo >&2 "Docker server is not running? type 'docker info'."; exit 1; }

if [ -t 0 ]; then DOCKER_ARGS=-it; fi
docker pull kszonek/clang-format-17

docker run --rm $DOCKER_ARGS -v "$MAINSOURCE":"$MAINSOURCE":Z -w "$MAINSOURCE" -u "$(id -u $USER):$(id -g $USER)" kszonek/clang-format-17 --style=file --verbose -i "$@" $ALL_ADA_FILES

Loading

0 comments on commit 0fa93be

Please sign in to comment.