Skip to content

Commit

Permalink
Fix snake case for 1d/2d/3d suffix (#617)
Browse files Browse the repository at this point in the history
Co-authored-by: Korijn van Golen <k.vangolen@clinicalgraphics.com>
  • Loading branch information
almarklein and Korijn authored Oct 14, 2024
1 parent 8842001 commit 52f6baa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
7 changes: 6 additions & 1 deletion codegen/tests/test_codegen_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ def test_to_snake_case():
assert to_snake_case("_foo_bar_spam") == "_foo_bar_spam"
assert to_snake_case("fooBarSpam") == "foo_bar_spam"
assert to_snake_case("_fooBarSpam") == "_foo_bar_spam"
assert to_snake_case("maxTextureDimension1D") == "max_texture_dimension1d"
assert to_snake_case("maxTextureDimension1D") == "max_texture_dimension_1d"
assert to_snake_case("max_texture_dimension_1d") == "max_texture_dimension_1d"


def test_to_camel_case():
assert to_camel_case("foo_bar_spam") == "fooBarSpam"
assert to_camel_case("_foo_bar_spam") == "_fooBarSpam"
assert to_camel_case("fooBarSpam") == "fooBarSpam"
assert to_camel_case("_fooBarSpam") == "_fooBarSpam"
assert to_camel_case("max_texture_dimension_1d") == "maxTextureDimension1D"
assert to_camel_case("max_texture_dimension1d") == "maxTextureDimension1D"
assert to_camel_case("max-texture-dimension1d") == "maxTextureDimension1D"
assert to_camel_case("max-texture-dimension-1d") == "maxTextureDimension1D"
assert to_camel_case("maxTextureDimension1D") == "maxTextureDimension1D"


def test_remove_c_comments():
Expand Down
12 changes: 8 additions & 4 deletions codegen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
import subprocess


def to_snake_case(name):
def to_snake_case(name, separator="_"):
"""Convert a name from camelCase to snake_case. Names that already are
snake_case remain the same.
"""
name2 = ""
for c in name:
c2 = c.lower()
if c2 != c and len(name2) > 0 and name2[-1] not in "_123":
name2 += "_"
if c2 != c and len(name2) > 0:
prev = name2[-1]
if c2 == "d" and prev in "123":
name2 = name2[:-1] + separator + prev
elif prev != separator:
name2 += separator
name2 += c2
return name2

Expand All @@ -28,7 +32,7 @@ def to_camel_case(name):
is_capital = False
name2 = ""
for c in name:
if c == "_" and name2:
if c in "_-" and name2:
is_capital = True
elif is_capital:
name2 += c.upper()
Expand Down
8 changes: 5 additions & 3 deletions wgpu/backends/wgpu_native/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def get_surface_id_from_info(present_info):
return lib.wgpuInstanceCreateSurface(get_wgpu_instance(), surface_descriptor)


# The functions below are copied from codegen/utils.py
# The functions below are copied from codegen/utils.py - let's keep these in sync!


def to_snake_case(name, separator="_"):
Expand All @@ -200,7 +200,9 @@ def to_snake_case(name, separator="_"):
c2 = c.lower()
if c2 != c and len(name2) > 0:
prev = name2[-1]
if prev not in "123" and prev != separator:
if c2 == "d" and prev in "123":
name2 = name2[:-1] + separator + prev
elif prev != separator:
name2 += separator
name2 += c2
return name2
Expand All @@ -213,7 +215,7 @@ def to_camel_case(name):
is_capital = False
name2 = ""
for c in name:
if c == "_" and name2:
if c in "_-" and name2:
is_capital = True
elif is_capital:
name2 += c.upper()
Expand Down

0 comments on commit 52f6baa

Please sign in to comment.