-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mycpp] Implement with str_switch(s) pattern
It first dispatches on the string length, and then the value with the runtime function str_equals_c().
- Loading branch information
Andy C
committed
Mar 11, 2024
1 parent
46f144b
commit 6efd0db
Showing
6 changed files
with
238 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#!/usr/bin/env python2 | ||
""" | ||
invalid_except.py | ||
""" | ||
from __future__ import print_function | ||
|
||
from mycpp.mylib import switch, str_switch, tagswitch | ||
|
||
|
||
def NoDefault(): | ||
# type: () -> None | ||
|
||
with switch(42) as case: | ||
if case(42): | ||
print('42') | ||
|
||
|
||
def TagSwitch(): | ||
# type: () -> None | ||
|
||
s = "foo" | ||
with tagswitch(s) as case: | ||
if 42: | ||
print('ONE') | ||
print('dupe') | ||
|
||
elif 43: | ||
print('TWO') | ||
|
||
else: | ||
print('neither') | ||
|
||
|
||
def SwitchMustHaveCase(): | ||
# type: () -> None | ||
|
||
i = 49 | ||
with switch(i) as case: | ||
if 42: | ||
print('ONE') | ||
print('dupe') | ||
|
||
elif 43: | ||
print('TWO') | ||
|
||
else: | ||
print('neither') | ||
|
||
|
||
def StrSwitchNoTuple(): | ||
# type: () -> None | ||
|
||
s = "foo" | ||
with str_switch(s) as case: | ||
# Problem: if you switch on length, do you duplicate the bogies | ||
if case('spam', 'different len'): | ||
print('ONE') | ||
print('dupe') | ||
|
||
elif case('foo'): | ||
print('TWO') | ||
|
||
else: | ||
print('neither') | ||
|
||
|
||
def StrSwitchNoInt(): | ||
# type: () -> None | ||
|
||
s = "foo" | ||
with str_switch(s) as case: | ||
# integer not allowed | ||
if case(42): | ||
print('ONE') | ||
print('dupe') | ||
|
||
else: | ||
print('neither') | ||
|
||
|
||
def run_tests(): | ||
# type: () -> None | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.