Skip to content

Commit

Permalink
Merge pull request #67 from lgrandco/fix-array_init
Browse files Browse the repository at this point in the history
array initialization fix
  • Loading branch information
cacharle authored Sep 3, 2023
2 parents 7f0d2e5 + 05d0dd8 commit 3ccc2ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
8 changes: 5 additions & 3 deletions c_formatter_42/formatters/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# helper.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: kiokuless <[email protected]> +#+ +:+ +#+ #
# By: root <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 11:38:00 by cacharle #+# #+# #
# Updated: 2023/07/17 02:28:52 by kiokuless ### ########.fr #
# Updated: 2023/09/03 11:05:57 by root ### ########.fr #
# #
# **************************************************************************** #
from __future__ import annotations
Expand All @@ -22,7 +22,9 @@
# 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}(\[.*\])*(\)\(.*\))?".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]:
Expand Down
20 changes: 16 additions & 4 deletions tests/formatters/test_align.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# ############################################################################ #
# **************************************************************************** #
# #
# ::: :::::::: #
# test_align.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cacharle <[email protected]> +#+ +:+ +#+ #
# By: root <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 12:19:45 by cacharle #+# #+# #
# Updated: 2021/02/11 20:35:54 by charles ### ########.fr #
# Updated: 2023/09/02 20:26:16 by root ### ########.fr #
# #
# ############################################################################ #
# **************************************************************************** #

import pytest

Expand Down Expand Up @@ -791,3 +791,15 @@ def test_align_func_typedef():
int\t\t\t\t\t\tbar();
"""
assert output == align(input)


def test_align_array_initialization():
input = """
int\ta;
static int\txs[4] = {1, 2, 3, 4};
"""
output = """
int\t\t\ta;
static int\txs[4] = {1, 2, 3, 4};
"""
assert output == align(input)
16 changes: 10 additions & 6 deletions tests/formatters/test_hoist.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# ############################################################################ #
# **************************************************************************** #
# #
# ::: :::::::: #
# test_hoist.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cacharle <[email protected]> +#+ +:+ +#+ #
# By: root <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/04 12:29:07 by cacharle #+# #+# #
# Updated: 2021/02/11 22:16:57 by charles ### ########.fr #
# Updated: 2023/09/02 22:43:13 by root ### ########.fr #
# #
# ############################################################################ #
# **************************************************************************** #

import pytest

Expand Down Expand Up @@ -201,13 +201,17 @@ def test_hoist_array_initialization():
input = """
void foo(void)
{
\tint xs[4] = {1, 2, 3, 4};
\tint a = 2;
\tstatic int\txs[4] = {1, 2, 3, 4};
}
"""
output = """
void foo(void)
{
\tint xs[4] = {1, 2, 3, 4};
\tint\ta;
\tstatic int\txs[4] = {1, 2, 3, 4};
\ta = 2;
}
"""
assert output == hoist(input)
Expand Down

0 comments on commit 3ccc2ad

Please sign in to comment.