Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix snake case for 1d/2d/3d suffix #617

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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