FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
emscripten/tools/optimizer/istring.h at universal-python · JavaScriptIOT/emscripten · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JavaScriptIOT
/
emscripten
Public
forked from
emscripten-core/emscripten
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
emscripten
/
tools
/
optimizer
/
istring.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (122 loc) · 3.59 KB
Breadcrumbs
emscripten
/
tools
/
optimizer
/
istring.h
Copy path
File metadata and controls
150 lines (122 loc) · 3.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
Interned String type, 100% interned on creation. Comparisons are always just a pointer comparison
#
include
<
unordered_set
>
#
include
<
unordered_map
>
#
include
<
set
>
#
include
<
string.h
>
#
include
<
stdint.h
>
#
include
<
stdlib.h
>
#
include
<
stdio.h
>
#
include
<
assert.h
>
namespace
cashew
{
struct
IString
{
const
char
*str;
static
size_t
hash_c
(
const
char
*str) {
//
see http://www.cse.yorku.ca/~oz/hash.html
unsigned
int
hash =
5381
;
int
c;
while
((c = *str++)) {
hash = ((hash <<
5
) + hash) ^ c;
}
return
(
size_t
)hash;
}
class
CStringHash
:
public
std
::hash<
const
char
*> {
public:
size_t
operator
()(
const
char
*str)
const
{
return
IString::hash_c
(str);
}
};
class
CStringEqual
:
public
std
::equal_to<
const
char
*> {
public:
bool
operator
()(
const
char
*x,
const
char
*y)
const
{
return
strcmp
(x, y) ==
0
;
}
};
IString
() : str(
nullptr
) {}
IString
(
const
char
*s,
bool
reuse=
true
) {
//
if reuse=true, then input is assumed to remain alive; not copied
set
(s, reuse);
}
void
set
(
const
char
*s,
bool
reuse=
true
) {
typedef
std::unordered_set<
const
char
*, CStringHash, CStringEqual> StringSet;
static
StringSet* strings =
new
StringSet
();
if
(reuse) {
auto
result = strings->
insert
(s);
//
if already present, does nothing
str = *(result.
first
);
}
else
{
auto
existing = strings->
find
(s);
if
(existing == strings->
end
()) {
char
*copy = (
char
*)
malloc
(
strlen
(s)+
1
);
//
XXX leaked
strcpy
(copy, s);
s = copy;
}
else
{
s = *existing;
}
strings->
insert
(s);
str = s;
}
}
void
set
(
const
IString &s) {
str = s.
str
;
}
bool
operator
==(
const
IString& other)
const
{
//
assert((str == other.str) == !strcmp(str, other.str));
return
str == other.
str
;
//
fast!
}
bool
operator
!=(
const
IString& other)
const
{
//
assert((str == other.str) == !strcmp(str, other.str));
return
str != other.
str
;
//
fast!
}
bool
operator
<(
const
IString& other)
const
{
return
strcmp
(str ? str :
"
"
, other.
str
? other.
str
:
"
"
) <
0
;
}
char
operator
[](
int
x)
const
{
return
str[x];
}
bool
operator
!()
const
{
//
no string, or empty string
return
!str || str[
0
] ==
0
;
}
const
char
*
c_str
()
const
{
return
str; }
bool
equals
(
const
char
*other)
const
{
return
!
strcmp
(str, other); }
bool
isNull
() {
return
str ==
nullptr
; }
};
}
//
namespace cashew
//
Utilities for creating hashmaps/sets over IStrings
namespace
std
{
template
<>
struct
hash
<cashew::IString> : public unary_function<cashew::IString,
size_t
> {
size_t
operator
()(
const
cashew::IString& str)
const
{
size_t
hash =
size_t
(str.
str
);
return
hash = ((hash <<
5
) + hash) ^
5381
;
/*
(hash * 33) ^ c
*/
}
};
template
<>
struct
equal_to
<cashew::IString> : public binary_function<cashew::IString, cashew::IString,
bool
> {
bool
operator
()(
const
cashew::IString& x,
const
cashew::IString& y)
const
{
return
x == y;
}
};
}
//
namespace std
namespace
cashew
{
//
IStringSet
class
IStringSet
:
public
std
::unordered_set<IString> {
public:
IStringSet
() {}
IStringSet
(
const
char
*init) {
//
comma-delimited list
int
size =
strlen
(init);
char
*curr =
new
char
[size+
1
];
//
leaked!
strcpy
(curr, init);
while
(
1
) {
char
*end =
strchr
(curr,
'
'
);
if
(end) *end =
0
;
insert
(curr);
if
(!end)
break
;
curr = end +
1
;
}
}
bool
has
(
const
IString& str) {
return
count
(str) >
0
;
}
};
class
IOrderedStringSet
:
public
std
::set<IString> {
public:
bool
has
(
const
IString& str) {
return
count
(str) >
0
;
}
};
}
//
namespace cashew
Back
|
FazBrowse Home
|
New Git URL