diff --git a/c_formatter_42/formatters/align.py b/c_formatter_42/formatters/align.py index f199a54..85b2822 100644 --- a/c_formatter_42/formatters/align.py +++ b/c_formatter_42/formatters/align.py @@ -91,8 +91,7 @@ def align_scope(content: str, scope: Literal["local", "global"]) -> str: # Minimum alignment required for each line min_alignment = max( - (len(prefix.expandtabs(4)) // 4 + 1 for _, prefix, _ in aligned), - default=1, + (len(prefix.expandtabs(4)) // 4 + 1 for _, prefix, _ in aligned), default=1 ) for i, prefix, suffix in aligned: alignment = len(prefix.expandtabs(4)) // 4 diff --git a/c_formatter_42/formatters/helper.py b/c_formatter_42/formatters/helper.py index bc22278..16f7be9 100644 --- a/c_formatter_42/formatters/helper.py +++ b/c_formatter_42/formatters/helper.py @@ -3,12 +3,13 @@ # ::: :::::::: # # helper.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: root +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:38:00 by cacharle #+# #+# # -# Updated: 2023/09/03 11:05:57 by root ### ########.fr # +# Updated: 2023/09/22 15:21:49 by leo ### ########.fr # # # # **************************************************************************** # + from __future__ import annotations import re @@ -22,9 +23,7 @@ # regex for a c variable/function name REGEX_NAME = r"\**[a-zA-Z_*()]\w*" # regex for a name in a declaration context (with array and function ptr) -REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s{{.*}})?(\)\(.*\))?".format( - name=REGEX_NAME -) +REGEX_DECL_NAME = r"\(?{name}(\[.*\])*(\s\=\s.*)?(\)\(.*\))?".format(name=REGEX_NAME) def locally_scoped(func: Callable[[str], str]) -> Callable[[str], str]: diff --git a/c_formatter_42/formatters/hoist.py b/c_formatter_42/formatters/hoist.py index 35d0c4b..8d12586 100644 --- a/c_formatter_42/formatters/hoist.py +++ b/c_formatter_42/formatters/hoist.py @@ -1,14 +1,14 @@ -# ############################################################################ # +# **************************************************************************** # # # # ::: :::::::: # # hoist.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: cacharle +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 11:16:28 by cacharle #+# #+# # -# Updated: 2021/02/11 20:13:29 by charles ### ########.fr # +# Updated: 2023/09/22 16:16:01 by leo ### ########.fr # # # -# ############################################################################ # +# **************************************************************************** # import re @@ -59,7 +59,11 @@ def hoist(content: str) -> str: # If line is a declaration + assignment on the same line, # create 2 new lines, one for the declaration and one for the assignment # NOTE: edge case for array declarations which can't be hoisted (See #56) - if m is not None and re.match(r".*\[.*\].*", m.group("name")) is None: + if ( + m is not None + and re.match(r".*\[.*\].*", m.group("name")) is None + and re.match(r"\s*(const|static)\s.*", line) is None + ): lines.append(f"\t{m.group('type')}\t{m.group('name')};") lines.append( "{}{} = {};".format( diff --git a/tests/formatters/test_align.py b/tests/formatters/test_align.py index b9274be..ccbdd8a 100644 --- a/tests/formatters/test_align.py +++ b/tests/formatters/test_align.py @@ -3,10 +3,10 @@ # ::: :::::::: # # test_align.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: root +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 12:19:45 by cacharle #+# #+# # -# Updated: 2023/09/02 20:26:16 by root ### ########.fr # +# Updated: 2023/09/23 11:06:39 by leo ### ########.fr # # # # **************************************************************************** # @@ -188,7 +188,7 @@ def test_align_local_multiple_functions(): output = """ int\t\t\tf() { -\tint a = 0; +\tint\ta = 0; } int\t\t\tg() { diff --git a/tests/formatters/test_hoist.py b/tests/formatters/test_hoist.py index b682cea..875b6f3 100644 --- a/tests/formatters/test_hoist.py +++ b/tests/formatters/test_hoist.py @@ -3,10 +3,10 @@ # ::: :::::::: # # test_hoist.py :+: :+: :+: # # +:+ +:+ +:+ # -# By: root +#+ +:+ +#+ # +# By: leo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/10/04 12:29:07 by cacharle #+# #+# # -# Updated: 2023/09/02 22:43:13 by root ### ########.fr # +# Updated: 2023/09/23 10:55:14 by leo ### ########.fr # # # # **************************************************************************** # @@ -197,12 +197,14 @@ def test_hoist_empty_function(): assert output == hoist(input) -def test_hoist_array_initialization(): +def test_hoist_initializations(): input = """ void foo(void) { \tint a = 2; \tstatic int\txs[4] = {1, 2, 3, 4}; +\tconst int\tb = 2; +\tlong int\tc = 2; } """ output = """ @@ -210,8 +212,11 @@ def test_hoist_array_initialization(): { \tint\ta; \tstatic int\txs[4] = {1, 2, 3, 4}; +\tconst int\tb = 2; +\tlong int\tc; \ta = 2; +\tc = 2; } """ assert output == hoist(input)