Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
http-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cython-plus
http-server
Commits
08a28634
Commit
08a28634
authored
Nov 09, 2021
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add strings.pyx demo
parent
b1628e1e
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
0 deletions
+72
-0
src/strings.pyx
src/strings.pyx
+72
-0
No files found.
src/strings.pyx
0 → 100644
View file @
08a28634
from
stdlib.string
cimport
Str
from
libcythonplus.dict
cimport
cypdict
from
libcythonplus.list
cimport
cyplist
from
libc.stdio
cimport
puts
,
printf
import
traceback
def
test_hash
():
with
nogil
:
s
=
Str
(
'hello'
)
h
=
s
.
__hash__
()
printf
(
"hash('hello') = %lu
\
n
"
,
h
)
d
=
cypdict
[
Str
,
int
]()
d
[
Str
(
'one'
)]
=
1
d
[
Str
(
'two'
)]
=
2
one
=
d
[
Str
(
'one'
)]
printf
(
"one: %d
\
n
"
,
one
)
def
test_split
():
with
nogil
:
s
=
Str
(
"Split this sentence . "
)
words
=
s
.
split
()
for
w
in
words
:
puts
(
Str
.
to_c_str
(
w
))
spaced
=
s
.
split
(
Str
(
' '
))
for
w
in
spaced
:
puts
(
Str
.
to_c_str
(
w
))
def
test_join
():
with
nogil
:
delimiter
=
Str
(
", "
)
elements
=
cyplist
[
Str
]()
elements
.
append
(
Str
(
"one"
))
elements
.
append
(
Str
(
"two"
))
elements
.
append
(
Str
(
"three"
))
elements
.
append
(
Str
(
"four"
))
joined
=
delimiter
.
join
(
elements
)
puts
(
Str
.
to_c_str
(
joined
))
def
test_conversions
():
with
nogil
:
number
=
Str
(
'42'
)
integer
=
<
int
>
number
printf
(
'%s -> %d
\
n
'
,
Str
.
to_c_str
(
number
),
integer
)
upper
=
Str
(
'Hello-World'
)
lower
=
upper
.
lower
()
printf
(
"%s -> %s
\
n
"
,
Str
.
to_c_str
(
upper
),
Str
.
to_c_str
(
lower
))
word
=
Str
(
'word'
)
# C++ exception not handled if conversion operator is called implicitly
# fail = <int> word
fail
=
word
.
__int__
()
def
main
():
for
f
in
(
test_split
,
test_join
,
test_hash
,
test_conversions
):
print
(
">>> %s()"
%
f
.
__name__
)
try
:
f
()
except
Exception
:
traceback
.
print_exc
()
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment