Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
a13167f9
Commit
a13167f9
authored
Aug 08, 2015
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
copy CPython string hash code plus some modifications
parent
dc25448a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
14 deletions
+55
-14
src/runtime/str.cpp
src/runtime/str.cpp
+55
-14
No files found.
src/runtime/str.cpp
View file @
a13167f9
...
@@ -1522,28 +1522,69 @@ failed:
...
@@ -1522,28 +1522,69 @@ failed:
}
}
extern
"C"
size_t
unicodeHashUnboxed
(
PyUnicodeObject
*
self
)
{
extern
"C"
size_t
unicodeHashUnboxed
(
PyUnicodeObject
*
self
)
{
Py_ssize_t
len
;
Py_UNICODE
*
p
;
long
x
;
#ifdef Py_DEBUG
assert
(
_Py_HashSecret_Initialized
);
#endif
if
(
self
->
hash
!=
-
1
)
if
(
self
->
hash
!=
-
1
)
return
self
->
hash
;
return
self
->
hash
;
len
=
PyUnicode_GET_SIZE
(
self
);
/*
We make the hash of the empty string be 0, rather than using
(prefix ^ suffix), since this slightly obfuscates the hash secret
*/
if
(
len
==
0
)
{
self
->
hash
=
0
;
return
0
;
}
p
=
PyUnicode_AS_UNICODE
(
self
);
x
=
_Py_HashSecret
.
prefix
;
x
^=
*
p
<<
7
;
while
(
--
len
>=
0
)
x
=
(
1000003
*
x
)
^
*
p
++
;
x
^=
PyUnicode_GET_SIZE
(
self
);
x
^=
_Py_HashSecret
.
suffix
;
if
(
x
==
-
1
)
x
=
-
2
;
self
->
hash
=
x
;
return
x
;
}
Py_ssize_t
len
=
PyUnicode_GET_SIZE
(
self
);
extern
"C"
size_t
strHashUnboxed
(
BoxedString
*
self
)
{
assert
(
PyString_Check
(
self
));
const
char
*
p
;
long
x
;
#ifdef Py_DEBUG
assert
(
_Py_HashSecret_Initialized
);
#endif
if
(
len
==
0
)
long
len
=
Py_SIZE
(
self
);
/*
We make the hash of the empty string be 0, rather than using
(prefix ^ suffix), since this slightly obfuscates the hash secret
*/
if
(
len
==
0
)
{
return
0
;
return
0
;
}
p
=
self
->
s
().
data
();
x
=
_Py_HashSecret
.
prefix
;
x
^=
*
p
<<
7
;
while
(
--
len
>=
0
)
x
=
(
1000003
*
x
)
^
*
p
++
;
x
^=
Py_SIZE
(
self
);
x
^=
_Py_HashSecret
.
suffix
;
if
(
x
==
-
1
)
x
=
-
2
;
Py_UNICODE
*
p
=
PyUnicode_AS_UNICODE
(
self
);
return
x
;
pyston
::
StringHash
<
Py_UNICODE
>
H
;
return
H
(
p
,
len
);
}
}
extern
"C"
Box
*
strHash
(
BoxedString
*
self
)
{
extern
"C"
Box
*
strHash
(
BoxedString
*
self
)
{
assert
(
PyString_Check
(
self
));
return
boxLong
(
strHashUnboxed
(
self
));
// CPython set the hash empty string to 0 manually
if
(
self
->
size
()
==
0
)
return
boxInt
(
0
);
StringHash
<
char
>
H
;
return
boxInt
(
H
(
self
->
data
(),
self
->
size
()));
}
}
extern
"C"
Box
*
strNonzero
(
BoxedString
*
self
)
{
extern
"C"
Box
*
strNonzero
(
BoxedString
*
self
)
{
...
@@ -2714,7 +2755,7 @@ void setupStr() {
...
@@ -2714,7 +2755,7 @@ void setupStr() {
str_cls
->
giveAttr
(
"__len__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strLen
,
BOXED_INT
,
1
)));
str_cls
->
giveAttr
(
"__len__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strLen
,
BOXED_INT
,
1
)));
str_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strStr
,
STR
,
1
)));
str_cls
->
giveAttr
(
"__str__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strStr
,
STR
,
1
)));
str_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strRepr
,
STR
,
1
)));
str_cls
->
giveAttr
(
"__repr__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strRepr
,
STR
,
1
)));
str_cls
->
giveAttr
(
"__hash__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strHash
,
BOXED_INT
,
1
)));
str_cls
->
giveAttr
(
"__hash__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strHash
,
UNKNOWN
,
1
)));
str_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strNonzero
,
BOXED_BOOL
,
1
)));
str_cls
->
giveAttr
(
"__nonzero__"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strNonzero
,
BOXED_BOOL
,
1
)));
str_cls
->
giveAttr
(
"isalnum"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsAlnum
,
BOXED_BOOL
,
1
)));
str_cls
->
giveAttr
(
"isalnum"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
strIsAlnum
,
BOXED_BOOL
,
1
)));
...
...
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