Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
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
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
Libraries
mruby
Commits
32e45016
Commit
32e45016
authored
Jun 07, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move STR_* macros to mruby/string.h with renaming
parent
b0c90141
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
153 additions
and
168 deletions
+153
-168
include/mruby/string.h
include/mruby/string.h
+32
-15
mrbgems/mruby-string-utf8/src/string.c
mrbgems/mruby-string-utf8/src/string.c
+6
-12
src/string.c
src/string.c
+115
-141
No files found.
include/mruby/string.h
View file @
32e45016
...
@@ -30,20 +30,37 @@ struct RString {
...
@@ -30,20 +30,37 @@ struct RString {
}
as
;
}
as
;
};
};
#define RSTR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
#define RSTR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
#define RSTR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
#define RSTR_SET_EMBED_LEN(s, n) do {\
size_t tmp_n = (n);\
s->flags &= ~MRB_STR_EMBED_LEN_MASK;\
s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
} while (0)
#define RSTR_SET_LEN(s, n) do {\
if (RSTR_EMBED_P(s)) {\
RSTR_SET_EMBED_LEN((s),(n));\
} else {\
s->as.heap.len = (mrb_int)(n);\
}\
} while (0)
#define RSTR_EMBED_LEN(s)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define RSTR_PTR(s) ((RSTR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
#define RSTR_LEN(s) ((RSTR_EMBED_P(s)) ? RSTR_EMBED_LEN(s) : (s)->as.heap.len)
#define RSTR_CAPA(s) (RSTR_EMBED_P(s) ? RSTRING_EMBED_LEN_MAX : (s)->as.heap.aux.capa)
#define RSTR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
#define RSTR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED)
#define RSTR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
#define RSTRING(s) ((struct RString*)(mrb_ptr(s)))
#define RSTRING(s) mrb_str_ptr(s)
#define RSTRING_PTR(s)\
#define RSTRING_PTR(s) RSTR_PTR(RSTRING(s))
((RSTRING(s)->flags & MRB_STR_EMBED) ?\
#define RSTRING_EMBED_LEN(s) RSTR_ENBED_LEN(RSTRING(s))
RSTRING(s)->as.ary :\
#define RSTRING_LEN(s) RSTR_LEN(RSTRING(s))
RSTRING(s)->as.heap.ptr)
#define RSTRING_CAPA(s) RSTR_CAPA(RSTRING(s))
#define RSTRING_LEN(s)\
((RSTRING(s)->flags & MRB_STR_EMBED) ?\
(mrb_int)((RSTRING(s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT) :\
RSTRING(s)->as.heap.len)
#define RSTRING_CAPA(s)\
((RSTRING(s)->flags & MRB_STR_EMBED) ?\
RSTRING_EMBED_LEN_MAX :\
RSTRING(s)->as.heap.aux.capa)
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
mrb_int
mrb_str_strlen
(
mrb_state
*
,
struct
RString
*
);
mrb_int
mrb_str_strlen
(
mrb_state
*
,
struct
RString
*
);
...
...
mrbgems/mruby-string-utf8/src/string.c
View file @
32e45016
...
@@ -7,12 +7,6 @@
...
@@ -7,12 +7,6 @@
#include <ctype.h>
#include <ctype.h>
#include <string.h>
#include <string.h>
#define STR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
#define STR_EMBED_LEN(s)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define STR_PTR(s) ((STR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
#define STR_LEN(s) ((STR_EMBED_P(s)) ? STR_EMBED_LEN(s) : (mrb_int)(s)->as.heap.len)
static
const
char
utf8len_codepage
[
256
]
=
static
const
char
utf8len_codepage
[
256
]
=
{
{
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
...
@@ -250,17 +244,17 @@ str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
...
@@ -250,17 +244,17 @@ str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
mrb_int
len
=
RSTRING_LEN
(
sub
);
mrb_int
len
=
RSTRING_LEN
(
sub
);
/* substring longer than string */
/* substring longer than string */
if
(
STR_LEN
(
ps
)
<
len
)
return
-
1
;
if
(
R
STR_LEN
(
ps
)
<
len
)
return
-
1
;
if
(
STR_LEN
(
ps
)
-
pos
<
len
)
{
if
(
R
STR_LEN
(
ps
)
-
pos
<
len
)
{
pos
=
STR_LEN
(
ps
)
-
len
;
pos
=
R
STR_LEN
(
ps
)
-
len
;
}
}
sbeg
=
STR_PTR
(
ps
);
sbeg
=
R
STR_PTR
(
ps
);
s
=
STR_PTR
(
ps
)
+
pos
;
s
=
R
STR_PTR
(
ps
)
+
pos
;
t
=
RSTRING_PTR
(
sub
);
t
=
RSTRING_PTR
(
sub
);
if
(
len
)
{
if
(
len
)
{
while
(
sbeg
<=
s
)
{
while
(
sbeg
<=
s
)
{
if
(
memcmp
(
s
,
t
,
len
)
==
0
)
{
if
(
memcmp
(
s
,
t
,
len
)
==
0
)
{
return
s
-
STR_PTR
(
ps
);
return
s
-
R
STR_PTR
(
ps
);
}
}
s
--
;
s
--
;
}
}
...
...
src/string.c
View file @
32e45016
...
@@ -17,28 +17,6 @@
...
@@ -17,28 +17,6 @@
#include "mruby/string.h"
#include "mruby/string.h"
#include "mruby/re.h"
#include "mruby/re.h"
#define STR_EMBED_P(s) ((s)->flags & MRB_STR_EMBED)
#define STR_SET_EMBED_FLAG(s) ((s)->flags |= MRB_STR_EMBED)
#define STR_UNSET_EMBED_FLAG(s) ((s)->flags &= ~(MRB_STR_EMBED|MRB_STR_EMBED_LEN_MASK))
#define STR_SET_EMBED_LEN(s, n) do {\
size_t tmp_n = (n);\
s->flags &= ~MRB_STR_EMBED_LEN_MASK;\
s->flags |= (tmp_n) << MRB_STR_EMBED_LEN_SHIFT;\
} while (0)
#define STR_SET_LEN(s, n) do {\
if (STR_EMBED_P(s)) {\
STR_SET_EMBED_LEN((s),(n));\
} else {\
s->as.heap.len = (mrb_int)(n);\
}\
} while (0)
#define RSTRING_EMBED_LEN(s) \
(mrb_int)((RSTRING(s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define STR_EMBED_LEN(s)\
(mrb_int)(((s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT)
#define STR_PTR(s) ((STR_EMBED_P(s)) ? (s)->as.ary : (s)->as.heap.ptr)
#define STR_LEN(s) ((STR_EMBED_P(s)) ? STR_EMBED_LEN(s) : (s)->as.heap.len)
const
char
mrb_digitmap
[]
=
"0123456789abcdefghijklmnopqrstuvwxyz"
;
const
char
mrb_digitmap
[]
=
"0123456789abcdefghijklmnopqrstuvwxyz"
;
typedef
struct
mrb_shared_string
{
typedef
struct
mrb_shared_string
{
...
@@ -48,18 +26,14 @@ typedef struct mrb_shared_string {
...
@@ -48,18 +26,14 @@ typedef struct mrb_shared_string {
mrb_int
len
;
mrb_int
len
;
}
mrb_shared_string
;
}
mrb_shared_string
;
#define STR_SHARED_P(s) ((s)->flags & MRB_STR_SHARED)
#define STR_SET_SHARED_FLAG(s) ((s)->flags |= MRB_STR_SHARED)
#define STR_UNSET_SHARED_FLAG(s) ((s)->flags &= ~MRB_STR_SHARED)
static
mrb_value
str_replace
(
mrb_state
*
mrb
,
struct
RString
*
s1
,
struct
RString
*
s2
);
static
mrb_value
str_replace
(
mrb_state
*
mrb
,
struct
RString
*
s1
,
struct
RString
*
s2
);
static
mrb_value
mrb_str_subseq
(
mrb_state
*
mrb
,
mrb_value
str
,
mrb_int
beg
,
mrb_int
len
);
static
mrb_value
mrb_str_subseq
(
mrb_state
*
mrb
,
mrb_value
str
,
mrb_int
beg
,
mrb_int
len
);
mrb_int
mrb_int
mrb_str_strlen
(
mrb_state
*
mrb
,
struct
RString
*
s
)
mrb_str_strlen
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
{
mrb_int
i
,
max
=
STR_LEN
(
s
);
mrb_int
i
,
max
=
R
STR_LEN
(
s
);
char
*
p
=
STR_PTR
(
s
);
char
*
p
=
R
STR_PTR
(
s
);
if
(
!
p
)
return
0
;
if
(
!
p
)
return
0
;
for
(
i
=
0
;
i
<
max
;
i
++
)
{
for
(
i
=
0
;
i
<
max
;
i
++
)
{
...
@@ -73,19 +47,19 @@ mrb_str_strlen(mrb_state *mrb, struct RString *s)
...
@@ -73,19 +47,19 @@ mrb_str_strlen(mrb_state *mrb, struct RString *s)
static
inline
void
static
inline
void
resize_capa
(
mrb_state
*
mrb
,
struct
RString
*
s
,
mrb_int
capacity
)
resize_capa
(
mrb_state
*
mrb
,
struct
RString
*
s
,
mrb_int
capacity
)
{
{
if
(
STR_EMBED_P
(
s
))
{
if
(
R
STR_EMBED_P
(
s
))
{
if
(
RSTRING_EMBED_LEN_MAX
<
capacity
)
{
if
(
RSTRING_EMBED_LEN_MAX
<
capacity
)
{
char
*
const
tmp
=
(
char
*
)
mrb_malloc
(
mrb
,
capacity
+
1
);
char
*
const
tmp
=
(
char
*
)
mrb_malloc
(
mrb
,
capacity
+
1
);
const
mrb_int
len
=
STR_EMBED_LEN
(
s
);
const
mrb_int
len
=
R
STR_EMBED_LEN
(
s
);
memcpy
(
tmp
,
s
->
as
.
ary
,
len
);
memcpy
(
tmp
,
s
->
as
.
ary
,
len
);
STR_UNSET_EMBED_FLAG
(
s
);
R
STR_UNSET_EMBED_FLAG
(
s
);
s
->
as
.
heap
.
ptr
=
tmp
;
s
->
as
.
heap
.
ptr
=
tmp
;
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
aux
.
capa
=
capacity
;
s
->
as
.
heap
.
aux
.
capa
=
capacity
;
}
}
}
}
else
{
else
{
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_realloc
(
mrb
,
STR_PTR
(
s
),
capacity
+
1
);
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_realloc
(
mrb
,
R
STR_PTR
(
s
),
capacity
+
1
);
s
->
as
.
heap
.
aux
.
capa
=
capacity
;
s
->
as
.
heap
.
aux
.
capa
=
capacity
;
}
}
}
}
...
@@ -105,20 +79,20 @@ str_decref(mrb_state *mrb, mrb_shared_string *shared)
...
@@ -105,20 +79,20 @@ str_decref(mrb_state *mrb, mrb_shared_string *shared)
void
void
mrb_str_modify
(
mrb_state
*
mrb
,
struct
RString
*
s
)
mrb_str_modify
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
{
if
(
STR_SHARED_P
(
s
))
{
if
(
R
STR_SHARED_P
(
s
))
{
mrb_shared_string
*
shared
=
s
->
as
.
heap
.
aux
.
shared
;
mrb_shared_string
*
shared
=
s
->
as
.
heap
.
aux
.
shared
;
if
(
shared
->
refcnt
==
1
&&
s
->
as
.
heap
.
ptr
==
shared
->
ptr
)
{
if
(
shared
->
refcnt
==
1
&&
s
->
as
.
heap
.
ptr
==
shared
->
ptr
)
{
s
->
as
.
heap
.
ptr
=
shared
->
ptr
;
s
->
as
.
heap
.
ptr
=
shared
->
ptr
;
s
->
as
.
heap
.
aux
.
capa
=
shared
->
len
;
s
->
as
.
heap
.
aux
.
capa
=
shared
->
len
;
STR_PTR
(
s
)[
s
->
as
.
heap
.
len
]
=
'\0'
;
R
STR_PTR
(
s
)[
s
->
as
.
heap
.
len
]
=
'\0'
;
mrb_free
(
mrb
,
shared
);
mrb_free
(
mrb
,
shared
);
}
}
else
{
else
{
char
*
ptr
,
*
p
;
char
*
ptr
,
*
p
;
mrb_int
len
;
mrb_int
len
;
p
=
STR_PTR
(
s
);
p
=
R
STR_PTR
(
s
);
len
=
s
->
as
.
heap
.
len
;
len
=
s
->
as
.
heap
.
len
;
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
(
size_t
)
len
+
1
);
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
(
size_t
)
len
+
1
);
if
(
p
)
{
if
(
p
)
{
...
@@ -129,7 +103,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
...
@@ -129,7 +103,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
s
->
as
.
heap
.
aux
.
capa
=
len
;
s
->
as
.
heap
.
aux
.
capa
=
len
;
str_decref
(
mrb
,
shared
);
str_decref
(
mrb
,
shared
);
}
}
STR_UNSET_SHARED_FLAG
(
s
);
R
STR_UNSET_SHARED_FLAG
(
s
);
return
;
return
;
}
}
if
(
s
->
flags
&
MRB_STR_NOFREE
)
{
if
(
s
->
flags
&
MRB_STR_NOFREE
)
{
...
@@ -137,9 +111,9 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
...
@@ -137,9 +111,9 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
(
size_t
)
s
->
as
.
heap
.
len
+
1
);
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
(
size_t
)
s
->
as
.
heap
.
len
+
1
);
if
(
p
)
{
if
(
p
)
{
memcpy
(
STR_PTR
(
s
),
p
,
s
->
as
.
heap
.
len
);
memcpy
(
R
STR_PTR
(
s
),
p
,
s
->
as
.
heap
.
len
);
}
}
STR_PTR
(
s
)[
s
->
as
.
heap
.
len
]
=
'\0'
;
R
STR_PTR
(
s
)[
s
->
as
.
heap
.
len
]
=
'\0'
;
s
->
as
.
heap
.
aux
.
capa
=
s
->
as
.
heap
.
len
;
s
->
as
.
heap
.
aux
.
capa
=
s
->
as
.
heap
.
len
;
s
->
flags
&=
~
MRB_STR_NOFREE
;
s
->
flags
&=
~
MRB_STR_NOFREE
;
return
;
return
;
...
@@ -153,13 +127,13 @@ mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len)
...
@@ -153,13 +127,13 @@ mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len)
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
slen
=
STR_LEN
(
s
);
slen
=
R
STR_LEN
(
s
);
if
(
len
!=
slen
)
{
if
(
len
!=
slen
)
{
if
(
slen
<
len
||
slen
-
len
>
256
)
{
if
(
slen
<
len
||
slen
-
len
>
256
)
{
resize_capa
(
mrb
,
s
,
len
);
resize_capa
(
mrb
,
s
,
len
);
}
}
STR_SET_LEN
(
s
,
len
);
R
STR_SET_LEN
(
s
,
len
);
STR_PTR
(
s
)[
len
]
=
'\0'
;
/* sentinel */
R
STR_PTR
(
s
)[
len
]
=
'\0'
;
/* sentinel */
}
}
return
str
;
return
str
;
}
}
...
@@ -173,8 +147,8 @@ str_new(mrb_state *mrb, const char *p, size_t len)
...
@@ -173,8 +147,8 @@ str_new(mrb_state *mrb, const char *p, size_t len)
s
=
mrb_obj_alloc_string
(
mrb
);
s
=
mrb_obj_alloc_string
(
mrb
);
if
(
len
<
RSTRING_EMBED_LEN_MAX
)
{
if
(
len
<
RSTRING_EMBED_LEN_MAX
)
{
STR_SET_EMBED_FLAG
(
s
);
R
STR_SET_EMBED_FLAG
(
s
);
STR_SET_EMBED_LEN
(
s
,
len
);
R
STR_SET_EMBED_LEN
(
s
,
len
);
if
(
p
)
{
if
(
p
)
{
memcpy
(
s
->
as
.
ary
,
p
,
len
);
memcpy
(
s
->
as
.
ary
,
p
,
len
);
}
}
...
@@ -189,7 +163,7 @@ str_new(mrb_state *mrb, const char *p, size_t len)
...
@@ -189,7 +163,7 @@ str_new(mrb_state *mrb, const char *p, size_t len)
memcpy
(
s
->
as
.
heap
.
ptr
,
p
,
len
);
memcpy
(
s
->
as
.
heap
.
ptr
,
p
,
len
);
}
}
}
}
STR_PTR
(
s
)[
len
]
=
'\0'
;
R
STR_PTR
(
s
)[
len
]
=
'\0'
;
return
s
;
return
s
;
}
}
...
@@ -228,7 +202,7 @@ mrb_str_buf_new(mrb_state *mrb, size_t capa)
...
@@ -228,7 +202,7 @@ mrb_str_buf_new(mrb_state *mrb, size_t capa)
s
->
as
.
heap
.
len
=
0
;
s
->
as
.
heap
.
len
=
0
;
s
->
as
.
heap
.
aux
.
capa
=
capa
;
s
->
as
.
heap
.
aux
.
capa
=
capa
;
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
capa
+
1
);
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_malloc
(
mrb
,
capa
+
1
);
STR_PTR
(
s
)[
0
]
=
'\0'
;
R
STR_PTR
(
s
)[
0
]
=
'\0'
;
return
mrb_obj_value
(
s
);
return
mrb_obj_value
(
s
);
}
}
...
@@ -242,19 +216,19 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len)
...
@@ -242,19 +216,19 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len)
if
(
len
==
0
)
return
;
if
(
len
==
0
)
return
;
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
if
(
ptr
>=
STR_PTR
(
s
)
&&
ptr
<=
STR_PTR
(
s
)
+
(
size_t
)
STR_LEN
(
s
))
{
if
(
ptr
>=
RSTR_PTR
(
s
)
&&
ptr
<=
RSTR_PTR
(
s
)
+
(
size_t
)
R
STR_LEN
(
s
))
{
off
=
ptr
-
STR_PTR
(
s
);
off
=
ptr
-
R
STR_PTR
(
s
);
}
}
if
(
STR_EMBED_P
(
s
))
if
(
R
STR_EMBED_P
(
s
))
capa
=
RSTRING_EMBED_LEN_MAX
;
capa
=
RSTRING_EMBED_LEN_MAX
;
else
else
capa
=
s
->
as
.
heap
.
aux
.
capa
;
capa
=
s
->
as
.
heap
.
aux
.
capa
;
if
(
STR_LEN
(
s
)
>=
MRB_INT_MAX
-
(
mrb_int
)
len
)
{
if
(
R
STR_LEN
(
s
)
>=
MRB_INT_MAX
-
(
mrb_int
)
len
)
{
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string size too big"
);
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string size too big"
);
}
}
total
=
STR_LEN
(
s
)
+
len
;
total
=
R
STR_LEN
(
s
)
+
len
;
if
(
capa
<=
total
)
{
if
(
capa
<=
total
)
{
while
(
total
>
capa
)
{
while
(
total
>
capa
)
{
if
(
capa
+
1
>=
MRB_INT_MAX
/
2
)
{
if
(
capa
+
1
>=
MRB_INT_MAX
/
2
)
{
...
@@ -266,12 +240,12 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len)
...
@@ -266,12 +240,12 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len)
resize_capa
(
mrb
,
s
,
capa
);
resize_capa
(
mrb
,
s
,
capa
);
}
}
if
(
off
!=
-
1
)
{
if
(
off
!=
-
1
)
{
ptr
=
STR_PTR
(
s
)
+
off
;
ptr
=
R
STR_PTR
(
s
)
+
off
;
}
}
memcpy
(
STR_PTR
(
s
)
+
STR_LEN
(
s
),
ptr
,
len
);
memcpy
(
RSTR_PTR
(
s
)
+
R
STR_LEN
(
s
),
ptr
,
len
);
mrb_assert_int_fit
(
size_t
,
total
,
mrb_int
,
MRB_INT_MAX
);
mrb_assert_int_fit
(
size_t
,
total
,
mrb_int
,
MRB_INT_MAX
);
STR_SET_LEN
(
s
,
total
);
R
STR_SET_LEN
(
s
,
total
);
STR_PTR
(
s
)[
total
]
=
'\0'
;
/* sentinel */
R
STR_PTR
(
s
)[
total
]
=
'\0'
;
/* sentinel */
}
}
mrb_value
mrb_value
...
@@ -324,9 +298,9 @@ mrb_str_new_static(mrb_state *mrb, const char *p, size_t len)
...
@@ -324,9 +298,9 @@ mrb_str_new_static(mrb_state *mrb, const char *p, size_t len)
void
void
mrb_gc_free_str
(
mrb_state
*
mrb
,
struct
RString
*
str
)
mrb_gc_free_str
(
mrb_state
*
mrb
,
struct
RString
*
str
)
{
{
if
(
STR_EMBED_P
(
str
))
if
(
R
STR_EMBED_P
(
str
))
/* no code */
;
/* no code */
;
else
if
(
STR_SHARED_P
(
str
))
else
if
(
R
STR_SHARED_P
(
str
))
str_decref
(
mrb
,
str
->
as
.
heap
.
aux
.
shared
);
str_decref
(
mrb
,
str
->
as
.
heap
.
aux
.
shared
);
else
if
((
str
->
flags
&
MRB_STR_NOFREE
)
==
0
)
else
if
((
str
->
flags
&
MRB_STR_NOFREE
)
==
0
)
mrb_free
(
mrb
,
str
->
as
.
heap
.
ptr
);
mrb_free
(
mrb
,
str
->
as
.
heap
.
ptr
);
...
@@ -342,25 +316,25 @@ mrb_str_to_cstr(mrb_state *mrb, mrb_value str0)
...
@@ -342,25 +316,25 @@ mrb_str_to_cstr(mrb_state *mrb, mrb_value str0)
}
}
s
=
str_new
(
mrb
,
RSTRING_PTR
(
str0
),
RSTRING_LEN
(
str0
));
s
=
str_new
(
mrb
,
RSTRING_PTR
(
str0
),
RSTRING_LEN
(
str0
));
if
((
strlen
(
STR_PTR
(
s
))
^
STR_LEN
(
s
))
!=
0
)
{
if
((
strlen
(
RSTR_PTR
(
s
))
^
R
STR_LEN
(
s
))
!=
0
)
{
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string contains null byte"
);
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"string contains null byte"
);
}
}
return
STR_PTR
(
s
);
return
R
STR_PTR
(
s
);
}
}
static
void
static
void
str_make_shared
(
mrb_state
*
mrb
,
struct
RString
*
s
)
str_make_shared
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
{
if
(
!
STR_SHARED_P
(
s
))
{
if
(
!
R
STR_SHARED_P
(
s
))
{
mrb_shared_string
*
shared
=
(
mrb_shared_string
*
)
mrb_malloc
(
mrb
,
sizeof
(
mrb_shared_string
));
mrb_shared_string
*
shared
=
(
mrb_shared_string
*
)
mrb_malloc
(
mrb
,
sizeof
(
mrb_shared_string
));
shared
->
refcnt
=
1
;
shared
->
refcnt
=
1
;
if
(
STR_EMBED_P
(
s
))
{
if
(
R
STR_EMBED_P
(
s
))
{
const
mrb_int
len
=
STR_EMBED_LEN
(
s
);
const
mrb_int
len
=
R
STR_EMBED_LEN
(
s
);
char
*
const
tmp
=
(
char
*
)
mrb_malloc
(
mrb
,
len
+
1
);
char
*
const
tmp
=
(
char
*
)
mrb_malloc
(
mrb
,
len
+
1
);
memcpy
(
tmp
,
s
->
as
.
ary
,
len
);
memcpy
(
tmp
,
s
->
as
.
ary
,
len
);
tmp
[
len
]
=
'\0'
;
tmp
[
len
]
=
'\0'
;
STR_UNSET_EMBED_FLAG
(
s
);
R
STR_UNSET_EMBED_FLAG
(
s
);
s
->
as
.
heap
.
ptr
=
tmp
;
s
->
as
.
heap
.
ptr
=
tmp
;
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
len
=
len
;
shared
->
nofree
=
FALSE
;
shared
->
nofree
=
FALSE
;
...
@@ -382,7 +356,7 @@ str_make_shared(mrb_state *mrb, struct RString *s)
...
@@ -382,7 +356,7 @@ str_make_shared(mrb_state *mrb, struct RString *s)
}
}
shared
->
len
=
s
->
as
.
heap
.
len
;
shared
->
len
=
s
->
as
.
heap
.
len
;
s
->
as
.
heap
.
aux
.
shared
=
shared
;
s
->
as
.
heap
.
aux
.
shared
=
shared
;
STR_SET_SHARED_FLAG
(
s
);
R
STR_SET_SHARED_FLAG
(
s
);
}
}
}
}
...
@@ -397,8 +371,8 @@ mrb_str_body(mrb_value str, int *len_p)
...
@@ -397,8 +371,8 @@ mrb_str_body(mrb_value str, int *len_p)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
*
len_p
=
STR_LEN
(
s
);
*
len_p
=
R
STR_LEN
(
s
);
return
STR_PTR
(
s
);
return
R
STR_PTR
(
s
);
}
}
/*
/*
...
@@ -418,14 +392,14 @@ mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other)
...
@@ -418,14 +392,14 @@ mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other)
other
=
mrb_str_to_str
(
mrb
,
other
);
other
=
mrb_str_to_str
(
mrb
,
other
);
}
}
s2
=
mrb_str_ptr
(
other
);
s2
=
mrb_str_ptr
(
other
);
len
=
STR_LEN
(
s1
)
+
STR_LEN
(
s2
);
len
=
RSTR_LEN
(
s1
)
+
R
STR_LEN
(
s2
);
if
(
RSTRING_CAPA
(
self
)
<
len
)
{
if
(
RSTRING_CAPA
(
self
)
<
len
)
{
resize_capa
(
mrb
,
s1
,
len
);
resize_capa
(
mrb
,
s1
,
len
);
}
}
memcpy
(
STR_PTR
(
s1
)
+
STR_LEN
(
s1
),
STR_PTR
(
s2
),
STR_LEN
(
s2
));
memcpy
(
RSTR_PTR
(
s1
)
+
RSTR_LEN
(
s1
),
RSTR_PTR
(
s2
),
R
STR_LEN
(
s2
));
STR_SET_LEN
(
s1
,
len
);
R
STR_SET_LEN
(
s1
,
len
);
STR_PTR
(
s1
)[
len
]
=
'\0'
;
R
STR_PTR
(
s1
)[
len
]
=
'\0'
;
}
}
/*
/*
...
@@ -441,9 +415,9 @@ mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b)
...
@@ -441,9 +415,9 @@ mrb_str_plus(mrb_state *mrb, mrb_value a, mrb_value b)
struct
RString
*
s2
=
mrb_str_ptr
(
b
);
struct
RString
*
s2
=
mrb_str_ptr
(
b
);
struct
RString
*
t
;
struct
RString
*
t
;
t
=
str_new
(
mrb
,
0
,
STR_LEN
(
s
)
+
STR_LEN
(
s2
));
t
=
str_new
(
mrb
,
0
,
RSTR_LEN
(
s
)
+
R
STR_LEN
(
s2
));
memcpy
(
STR_PTR
(
t
),
STR_PTR
(
s
),
STR_LEN
(
s
));
memcpy
(
RSTR_PTR
(
t
),
RSTR_PTR
(
s
),
R
STR_LEN
(
s
));
memcpy
(
STR_PTR
(
t
)
+
STR_LEN
(
s
),
STR_PTR
(
s2
),
STR_LEN
(
s2
));
memcpy
(
RSTR_PTR
(
t
)
+
RSTR_LEN
(
s
),
RSTR_PTR
(
s2
),
R
STR_LEN
(
s2
));
return
mrb_obj_value
(
t
);
return
mrb_obj_value
(
t
);
}
}
...
@@ -475,7 +449,7 @@ static mrb_value
...
@@ -475,7 +449,7 @@ static mrb_value
mrb_str_bytesize
(
mrb_state
*
mrb
,
mrb_value
self
)
mrb_str_bytesize
(
mrb_state
*
mrb
,
mrb_value
self
)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
self
);
struct
RString
*
s
=
mrb_str_ptr
(
self
);
return
mrb_fixnum_value
(
STR_LEN
(
s
));
return
mrb_fixnum_value
(
R
STR_LEN
(
s
));
}
}
/* 15.2.10.5.26 */
/* 15.2.10.5.26 */
...
@@ -490,7 +464,7 @@ mrb_value
...
@@ -490,7 +464,7 @@ mrb_value
mrb_str_size
(
mrb_state
*
mrb
,
mrb_value
self
)
mrb_str_size
(
mrb_state
*
mrb
,
mrb_value
self
)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
self
);
struct
RString
*
s
=
mrb_str_ptr
(
self
);
return
mrb_fixnum_value
(
STR_LEN
(
s
));
return
mrb_fixnum_value
(
R
STR_LEN
(
s
));
}
}
/* 15.2.10.5.1 */
/* 15.2.10.5.1 */
...
@@ -521,7 +495,7 @@ mrb_str_times(mrb_state *mrb, mrb_value self)
...
@@ -521,7 +495,7 @@ mrb_str_times(mrb_state *mrb, mrb_value self)
len
=
RSTRING_LEN
(
self
)
*
times
;
len
=
RSTRING_LEN
(
self
)
*
times
;
str2
=
str_new
(
mrb
,
0
,
len
);
str2
=
str_new
(
mrb
,
0
,
len
);
str_with_class
(
mrb
,
str2
,
self
);
str_with_class
(
mrb
,
str2
,
self
);
p
=
STR_PTR
(
str2
);
p
=
R
STR_PTR
(
str2
);
if
(
len
>
0
)
{
if
(
len
>
0
)
{
n
=
RSTRING_LEN
(
self
);
n
=
RSTRING_LEN
(
self
);
memcpy
(
p
,
RSTRING_PTR
(
self
),
n
);
memcpy
(
p
,
RSTRING_PTR
(
self
),
n
);
...
@@ -531,7 +505,7 @@ mrb_str_times(mrb_state *mrb, mrb_value self)
...
@@ -531,7 +505,7 @@ mrb_str_times(mrb_state *mrb, mrb_value self)
}
}
memcpy
(
p
+
n
,
p
,
len
-
n
);
memcpy
(
p
+
n
,
p
,
len
-
n
);
}
}
p
[
STR_LEN
(
str2
)]
=
'\0'
;
p
[
R
STR_LEN
(
str2
)]
=
'\0'
;
return
mrb_obj_value
(
str2
);
return
mrb_obj_value
(
str2
);
}
}
...
@@ -555,11 +529,11 @@ mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2)
...
@@ -555,11 +529,11 @@ mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2)
struct
RString
*
s1
=
mrb_str_ptr
(
str1
);
struct
RString
*
s1
=
mrb_str_ptr
(
str1
);
struct
RString
*
s2
=
mrb_str_ptr
(
str2
);
struct
RString
*
s2
=
mrb_str_ptr
(
str2
);
len
=
lesser
(
STR_LEN
(
s1
),
STR_LEN
(
s2
));
len
=
lesser
(
RSTR_LEN
(
s1
),
R
STR_LEN
(
s2
));
retval
=
memcmp
(
STR_PTR
(
s1
),
STR_PTR
(
s2
),
len
);
retval
=
memcmp
(
RSTR_PTR
(
s1
),
R
STR_PTR
(
s2
),
len
);
if
(
retval
==
0
)
{
if
(
retval
==
0
)
{
if
(
STR_LEN
(
s1
)
==
STR_LEN
(
s2
))
return
0
;
if
(
RSTR_LEN
(
s1
)
==
R
STR_LEN
(
s2
))
return
0
;
if
(
STR_LEN
(
s1
)
>
STR_LEN
(
s2
))
return
1
;
if
(
RSTR_LEN
(
s1
)
>
R
STR_LEN
(
s2
))
return
1
;
return
-
1
;
return
-
1
;
}
}
if
(
retval
>
0
)
return
1
;
if
(
retval
>
0
)
return
1
;
...
@@ -913,8 +887,8 @@ mrb_str_capitalize_bang(mrb_state *mrb, mrb_value str)
...
@@ -913,8 +887,8 @@ mrb_str_capitalize_bang(mrb_state *mrb, mrb_value str)
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
if
(
STR_LEN
(
s
)
==
0
||
!
STR_PTR
(
s
))
return
mrb_nil_value
();
if
(
RSTR_LEN
(
s
)
==
0
||
!
R
STR_PTR
(
s
))
return
mrb_nil_value
();
p
=
STR_PTR
(
s
);
pend
=
STR_PTR
(
s
)
+
STR_LEN
(
s
);
p
=
RSTR_PTR
(
s
);
pend
=
RSTR_PTR
(
s
)
+
R
STR_LEN
(
s
);
if
(
ISLOWER
(
*
p
))
{
if
(
ISLOWER
(
*
p
))
{
*
p
=
TOUPPER
(
*
p
);
*
p
=
TOUPPER
(
*
p
);
modify
=
TRUE
;
modify
=
TRUE
;
...
@@ -970,29 +944,29 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
...
@@ -970,29 +944,29 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
len
=
STR_LEN
(
s
);
len
=
R
STR_LEN
(
s
);
if
(
mrb_get_args
(
mrb
,
"|S"
,
&
rs
)
==
0
)
{
if
(
mrb_get_args
(
mrb
,
"|S"
,
&
rs
)
==
0
)
{
if
(
len
==
0
)
return
mrb_nil_value
();
if
(
len
==
0
)
return
mrb_nil_value
();
smart_chomp:
smart_chomp:
if
(
STR_PTR
(
s
)[
len
-
1
]
==
'\n'
)
{
if
(
R
STR_PTR
(
s
)[
len
-
1
]
==
'\n'
)
{
STR_SET_LEN
(
s
,
STR_LEN
(
s
)
-
1
);
RSTR_SET_LEN
(
s
,
R
STR_LEN
(
s
)
-
1
);
if
(
STR_LEN
(
s
)
>
0
&&
if
(
R
STR_LEN
(
s
)
>
0
&&
STR_PTR
(
s
)[
STR_LEN
(
s
)
-
1
]
==
'\r'
)
{
RSTR_PTR
(
s
)[
R
STR_LEN
(
s
)
-
1
]
==
'\r'
)
{
STR_SET_LEN
(
s
,
STR_LEN
(
s
)
-
1
);
RSTR_SET_LEN
(
s
,
R
STR_LEN
(
s
)
-
1
);
}
}
}
}
else
if
(
STR_PTR
(
s
)[
len
-
1
]
==
'\r'
)
{
else
if
(
R
STR_PTR
(
s
)[
len
-
1
]
==
'\r'
)
{
STR_SET_LEN
(
s
,
STR_LEN
(
s
)
-
1
);
RSTR_SET_LEN
(
s
,
R
STR_LEN
(
s
)
-
1
);
}
}
else
{
else
{
return
mrb_nil_value
();
return
mrb_nil_value
();
}
}
STR_PTR
(
s
)[
STR_LEN
(
s
)]
=
'\0'
;
RSTR_PTR
(
s
)[
R
STR_LEN
(
s
)]
=
'\0'
;
return
str
;
return
str
;
}
}
if
(
len
==
0
||
mrb_nil_p
(
rs
))
return
mrb_nil_value
();
if
(
len
==
0
||
mrb_nil_p
(
rs
))
return
mrb_nil_value
();
p
=
STR_PTR
(
s
);
p
=
R
STR_PTR
(
s
);
rslen
=
RSTRING_LEN
(
rs
);
rslen
=
RSTRING_LEN
(
rs
);
if
(
rslen
==
0
)
{
if
(
rslen
==
0
)
{
while
(
len
>
0
&&
p
[
len
-
1
]
==
'\n'
)
{
while
(
len
>
0
&&
p
[
len
-
1
]
==
'\n'
)
{
...
@@ -1000,8 +974,8 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
...
@@ -1000,8 +974,8 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
if
(
len
>
0
&&
p
[
len
-
1
]
==
'\r'
)
if
(
len
>
0
&&
p
[
len
-
1
]
==
'\r'
)
len
--
;
len
--
;
}
}
if
(
len
<
STR_LEN
(
s
))
{
if
(
len
<
R
STR_LEN
(
s
))
{
STR_SET_LEN
(
s
,
len
);
R
STR_SET_LEN
(
s
,
len
);
p
[
len
]
=
'\0'
;
p
[
len
]
=
'\0'
;
return
str
;
return
str
;
}
}
...
@@ -1018,8 +992,8 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
...
@@ -1018,8 +992,8 @@ mrb_str_chomp_bang(mrb_state *mrb, mrb_value str)
if
(
p
[
len
-
1
]
==
newline
&&
if
(
p
[
len
-
1
]
==
newline
&&
(
rslen
<=
1
||
(
rslen
<=
1
||
memcmp
(
RSTRING_PTR
(
rs
),
pp
,
rslen
)
==
0
))
{
memcmp
(
RSTRING_PTR
(
rs
),
pp
,
rslen
)
==
0
))
{
STR_SET_LEN
(
s
,
len
-
rslen
);
R
STR_SET_LEN
(
s
,
len
-
rslen
);
p
[
STR_LEN
(
s
)]
=
'\0'
;
p
[
R
STR_LEN
(
s
)]
=
'\0'
;
return
str
;
return
str
;
}
}
return
mrb_nil_value
();
return
mrb_nil_value
();
...
@@ -1069,17 +1043,17 @@ mrb_str_chop_bang(mrb_state *mrb, mrb_value str)
...
@@ -1069,17 +1043,17 @@ mrb_str_chop_bang(mrb_state *mrb, mrb_value str)
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
if
(
STR_LEN
(
s
)
>
0
)
{
if
(
R
STR_LEN
(
s
)
>
0
)
{
mrb_int
len
;
mrb_int
len
;
len
=
STR_LEN
(
s
)
-
1
;
len
=
R
STR_LEN
(
s
)
-
1
;
if
(
STR_PTR
(
s
)[
len
]
==
'\n'
)
{
if
(
R
STR_PTR
(
s
)[
len
]
==
'\n'
)
{
if
(
len
>
0
&&
if
(
len
>
0
&&
STR_PTR
(
s
)[
len
-
1
]
==
'\r'
)
{
R
STR_PTR
(
s
)[
len
-
1
]
==
'\r'
)
{
len
--
;
len
--
;
}
}
}
}
STR_SET_LEN
(
s
,
len
);
R
STR_SET_LEN
(
s
,
len
);
STR_PTR
(
s
)[
len
]
=
'\0'
;
R
STR_PTR
(
s
)[
len
]
=
'\0'
;
return
str
;
return
str
;
}
}
return
mrb_nil_value
();
return
mrb_nil_value
();
...
@@ -1127,8 +1101,8 @@ mrb_str_downcase_bang(mrb_state *mrb, mrb_value str)
...
@@ -1127,8 +1101,8 @@ mrb_str_downcase_bang(mrb_state *mrb, mrb_value str)
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
p
=
STR_PTR
(
s
);
p
=
R
STR_PTR
(
s
);
pend
=
STR_PTR
(
s
)
+
STR_LEN
(
s
);
pend
=
RSTR_PTR
(
s
)
+
R
STR_LEN
(
s
);
while
(
p
<
pend
)
{
while
(
p
<
pend
)
{
if
(
ISUPPER
(
*
p
))
{
if
(
ISUPPER
(
*
p
))
{
*
p
=
TOLOWER
(
*
p
);
*
p
=
TOLOWER
(
*
p
);
...
@@ -1177,7 +1151,7 @@ mrb_str_empty_p(mrb_state *mrb, mrb_value self)
...
@@ -1177,7 +1151,7 @@ mrb_str_empty_p(mrb_state *mrb, mrb_value self)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
self
);
struct
RString
*
s
=
mrb_str_ptr
(
self
);
return
mrb_bool_value
(
STR_LEN
(
s
)
==
0
);
return
mrb_bool_value
(
R
STR_LEN
(
s
)
==
0
);
}
}
/* 15.2.10.5.17 */
/* 15.2.10.5.17 */
...
@@ -1206,7 +1180,7 @@ mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
...
@@ -1206,7 +1180,7 @@ mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
mrb_shared_string
*
shared
;
mrb_shared_string
*
shared
;
orig
=
mrb_str_ptr
(
str
);
orig
=
mrb_str_ptr
(
str
);
if
(
STR_EMBED_P
(
orig
))
{
if
(
R
STR_EMBED_P
(
orig
))
{
s
=
str_new
(
mrb
,
orig
->
as
.
ary
+
beg
,
len
);
s
=
str_new
(
mrb
,
orig
->
as
.
ary
+
beg
,
len
);
}
else
{
}
else
{
str_make_shared
(
mrb
,
orig
);
str_make_shared
(
mrb
,
orig
);
...
@@ -1215,7 +1189,7 @@ mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
...
@@ -1215,7 +1189,7 @@ mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
s
->
as
.
heap
.
ptr
=
orig
->
as
.
heap
.
ptr
+
beg
;
s
->
as
.
heap
.
ptr
=
orig
->
as
.
heap
.
ptr
+
beg
;
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
aux
.
shared
=
shared
;
s
->
as
.
heap
.
aux
.
shared
=
shared
;
STR_SET_SHARED_FLAG
(
s
);
R
STR_SET_SHARED_FLAG
(
s
);
shared
->
refcnt
++
;
shared
->
refcnt
++
;
}
}
...
@@ -1247,8 +1221,8 @@ mrb_str_hash(mrb_state *mrb, mrb_value str)
...
@@ -1247,8 +1221,8 @@ mrb_str_hash(mrb_state *mrb, mrb_value str)
{
{
/* 1-8-7 */
/* 1-8-7 */
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_int
len
=
STR_LEN
(
s
);
mrb_int
len
=
R
STR_LEN
(
s
);
char
*
p
=
STR_PTR
(
s
);
char
*
p
=
R
STR_PTR
(
s
);
mrb_int
key
=
0
;
mrb_int
key
=
0
;
while
(
len
--
)
{
while
(
len
--
)
{
...
@@ -1393,27 +1367,27 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2)
...
@@ -1393,27 +1367,27 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2)
{
{
long
len
;
long
len
;
len
=
STR_LEN
(
s2
);
len
=
R
STR_LEN
(
s2
);
if
(
STR_SHARED_P
(
s2
))
{
if
(
R
STR_SHARED_P
(
s2
))
{
L_SHARE:
L_SHARE:
if
(
STR_SHARED_P
(
s1
))
{
if
(
R
STR_SHARED_P
(
s1
))
{
str_decref
(
mrb
,
s1
->
as
.
heap
.
aux
.
shared
);
str_decref
(
mrb
,
s1
->
as
.
heap
.
aux
.
shared
);
}
}
else
if
(
!
STR_EMBED_P
(
s1
)
&&
!
(
s1
->
flags
&
MRB_STR_NOFREE
))
{
else
if
(
!
R
STR_EMBED_P
(
s1
)
&&
!
(
s1
->
flags
&
MRB_STR_NOFREE
))
{
mrb_free
(
mrb
,
s1
->
as
.
heap
.
ptr
);
mrb_free
(
mrb
,
s1
->
as
.
heap
.
ptr
);
}
}
STR_UNSET_EMBED_FLAG
(
s1
);
R
STR_UNSET_EMBED_FLAG
(
s1
);
s1
->
as
.
heap
.
ptr
=
s2
->
as
.
heap
.
ptr
;
s1
->
as
.
heap
.
ptr
=
s2
->
as
.
heap
.
ptr
;
s1
->
as
.
heap
.
len
=
len
;
s1
->
as
.
heap
.
len
=
len
;
s1
->
as
.
heap
.
aux
.
shared
=
s2
->
as
.
heap
.
aux
.
shared
;
s1
->
as
.
heap
.
aux
.
shared
=
s2
->
as
.
heap
.
aux
.
shared
;
STR_SET_SHARED_FLAG
(
s1
);
R
STR_SET_SHARED_FLAG
(
s1
);
s1
->
as
.
heap
.
aux
.
shared
->
refcnt
++
;
s1
->
as
.
heap
.
aux
.
shared
->
refcnt
++
;
}
}
else
{
else
{
if
(
len
<=
RSTRING_EMBED_LEN_MAX
)
{
if
(
len
<=
RSTRING_EMBED_LEN_MAX
)
{
STR_SET_EMBED_FLAG
(
s1
);
R
STR_SET_EMBED_FLAG
(
s1
);
memcpy
(
s1
->
as
.
ary
,
STR_PTR
(
s2
),
len
);
memcpy
(
s1
->
as
.
ary
,
R
STR_PTR
(
s2
),
len
);
STR_SET_EMBED_LEN
(
s1
,
len
);
R
STR_SET_EMBED_LEN
(
s1
,
len
);
}
}
else
{
else
{
str_make_shared
(
mrb
,
s2
);
str_make_shared
(
mrb
,
s2
);
...
@@ -1510,7 +1484,7 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
...
@@ -1510,7 +1484,7 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
uintptr_t
n
=
(
uintptr_t
)
p
;
uintptr_t
n
=
(
uintptr_t
)
p
;
p_str
=
str_new
(
mrb
,
NULL
,
2
+
sizeof
(
uintptr_t
)
*
CHAR_BIT
/
4
);
p_str
=
str_new
(
mrb
,
NULL
,
2
+
sizeof
(
uintptr_t
)
*
CHAR_BIT
/
4
);
p1
=
STR_PTR
(
p_str
);
p1
=
R
STR_PTR
(
p_str
);
*
p1
++
=
'0'
;
*
p1
++
=
'0'
;
*
p1
++
=
'x'
;
*
p1
++
=
'x'
;
p2
=
p1
;
p2
=
p1
;
...
@@ -1520,7 +1494,7 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
...
@@ -1520,7 +1494,7 @@ mrb_ptr_to_str(mrb_state *mrb, void *p)
n
/=
16
;
n
/=
16
;
}
while
(
n
>
0
);
}
while
(
n
>
0
);
*
p2
=
'\0'
;
*
p2
=
'\0'
;
STR_SET_LEN
(
p_str
,
(
mrb_int
)(
p2
-
STR_PTR
(
p_str
)));
RSTR_SET_LEN
(
p_str
,
(
mrb_int
)(
p2
-
R
STR_PTR
(
p_str
)));
while
(
p1
<
p2
)
{
while
(
p1
<
p2
)
{
const
char
c
=
*
p1
;
const
char
c
=
*
p1
;
...
@@ -1564,7 +1538,7 @@ mrb_str_reverse(mrb_state *mrb, mrb_value str)
...
@@ -1564,7 +1538,7 @@ mrb_str_reverse(mrb_state *mrb, mrb_value str)
s2
=
str_new
(
mrb
,
0
,
RSTRING_LEN
(
str
));
s2
=
str_new
(
mrb
,
0
,
RSTRING_LEN
(
str
));
str_with_class
(
mrb
,
s2
,
str
);
str_with_class
(
mrb
,
s2
,
str
);
s
=
RSTRING_PTR
(
str
);
e
=
RSTRING_END
(
str
)
-
1
;
s
=
RSTRING_PTR
(
str
);
e
=
RSTRING_END
(
str
)
-
1
;
p
=
STR_PTR
(
s2
);
p
=
R
STR_PTR
(
s2
);
while
(
e
>=
s
)
{
while
(
e
>=
s
)
{
*
p
++
=
*
e
--
;
*
p
++
=
*
e
--
;
...
@@ -1587,9 +1561,9 @@ mrb_str_reverse_bang(mrb_state *mrb, mrb_value str)
...
@@ -1587,9 +1561,9 @@ mrb_str_reverse_bang(mrb_state *mrb, mrb_value str)
char
c
;
char
c
;
mrb_str_modify
(
mrb
,
s
);
mrb_str_modify
(
mrb
,
s
);
if
(
STR_LEN
(
s
)
>
1
)
{
if
(
R
STR_LEN
(
s
)
>
1
)
{
p
=
STR_PTR
(
s
);
p
=
R
STR_PTR
(
s
);
e
=
p
+
STR_LEN
(
s
)
-
1
;
e
=
p
+
R
STR_LEN
(
s
)
-
1
;
while
(
p
<
e
)
{
while
(
p
<
e
)
{
c
=
*
p
;
c
=
*
p
;
*
p
++
=
*
e
;
*
p
++
=
*
e
;
...
@@ -1625,17 +1599,17 @@ mrb_str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
...
@@ -1625,17 +1599,17 @@ mrb_str_rindex(mrb_state *mrb, mrb_value str, mrb_value sub, mrb_int pos)
mrb_int
len
=
RSTRING_LEN
(
sub
);
mrb_int
len
=
RSTRING_LEN
(
sub
);
/* substring longer than string */
/* substring longer than string */
if
(
STR_LEN
(
ps
)
<
len
)
return
-
1
;
if
(
R
STR_LEN
(
ps
)
<
len
)
return
-
1
;
if
(
STR_LEN
(
ps
)
-
pos
<
len
)
{
if
(
R
STR_LEN
(
ps
)
-
pos
<
len
)
{
pos
=
STR_LEN
(
ps
)
-
len
;
pos
=
R
STR_LEN
(
ps
)
-
len
;
}
}
sbeg
=
STR_PTR
(
ps
);
sbeg
=
R
STR_PTR
(
ps
);
s
=
STR_PTR
(
ps
)
+
pos
;
s
=
R
STR_PTR
(
ps
)
+
pos
;
t
=
RSTRING_PTR
(
sub
);
t
=
RSTRING_PTR
(
sub
);
if
(
len
)
{
if
(
len
)
{
while
(
sbeg
<=
s
)
{
while
(
sbeg
<=
s
)
{
if
(
memcmp
(
s
,
t
,
len
)
==
0
)
{
if
(
memcmp
(
s
,
t
,
len
)
==
0
)
{
return
s
-
STR_PTR
(
ps
);
return
s
-
R
STR_PTR
(
ps
);
}
}
s
--
;
s
--
;
}
}
...
@@ -2047,11 +2021,11 @@ mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr)
...
@@ -2047,11 +2021,11 @@ mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr)
{
{
struct
RString
*
ps
=
mrb_str_ptr
(
*
ptr
);
struct
RString
*
ps
=
mrb_str_ptr
(
*
ptr
);
mrb_int
len
=
mrb_str_strlen
(
mrb
,
ps
);
mrb_int
len
=
mrb_str_strlen
(
mrb
,
ps
);
char
*
p
=
STR_PTR
(
ps
);
char
*
p
=
R
STR_PTR
(
ps
);
if
(
!
p
||
p
[
len
]
!=
'\0'
)
{
if
(
!
p
||
p
[
len
]
!=
'\0'
)
{
mrb_str_modify
(
mrb
,
ps
);
mrb_str_modify
(
mrb
,
ps
);
return
STR_PTR
(
ps
);
return
R
STR_PTR
(
ps
);
}
}
return
p
;
return
p
;
}
}
...
@@ -2073,7 +2047,7 @@ mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck)
...
@@ -2073,7 +2047,7 @@ mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck)
len
=
RSTRING_LEN
(
str
);
len
=
RSTRING_LEN
(
str
);
if
(
s
[
len
])
{
/* no sentinel somehow */
if
(
s
[
len
])
{
/* no sentinel somehow */
struct
RString
*
temp_str
=
str_new
(
mrb
,
s
,
len
);
struct
RString
*
temp_str
=
str_new
(
mrb
,
s
,
len
);
s
=
STR_PTR
(
temp_str
);
s
=
R
STR_PTR
(
temp_str
);
}
}
}
}
return
mrb_cstr_to_inum
(
mrb
,
s
,
base
,
badcheck
);
return
mrb_cstr_to_inum
(
mrb
,
s
,
base
,
badcheck
);
...
@@ -2193,7 +2167,7 @@ mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck)
...
@@ -2193,7 +2167,7 @@ mrb_str_to_dbl(mrb_state *mrb, mrb_value str, mrb_bool badcheck)
}
}
if
(
s
[
len
])
{
/* no sentinel somehow */
if
(
s
[
len
])
{
/* no sentinel somehow */
struct
RString
*
temp_str
=
str_new
(
mrb
,
s
,
len
);
struct
RString
*
temp_str
=
str_new
(
mrb
,
s
,
len
);
s
=
STR_PTR
(
temp_str
);
s
=
R
STR_PTR
(
temp_str
);
}
}
}
}
return
mrb_cstr_to_dbl
(
mrb
,
s
,
badcheck
);
return
mrb_cstr_to_dbl
(
mrb
,
s
,
badcheck
);
...
@@ -2334,7 +2308,7 @@ mrb_str_dump(mrb_state *mrb, mrb_value str)
...
@@ -2334,7 +2308,7 @@ mrb_str_dump(mrb_state *mrb, mrb_value str)
result
=
str_new
(
mrb
,
0
,
len
);
result
=
str_new
(
mrb
,
0
,
len
);
str_with_class
(
mrb
,
result
,
str
);
str_with_class
(
mrb
,
result
,
str
);
p
=
RSTRING_PTR
(
str
);
pend
=
p
+
RSTRING_LEN
(
str
);
p
=
RSTRING_PTR
(
str
);
pend
=
p
+
RSTRING_LEN
(
str
);
q
=
STR_PTR
(
result
);
q
=
R
STR_PTR
(
result
);
*
q
++
=
'"'
;
*
q
++
=
'"'
;
while
(
p
<
pend
)
{
while
(
p
<
pend
)
{
unsigned
char
c
=
*
p
++
;
unsigned
char
c
=
*
p
++
;
...
@@ -2513,8 +2487,8 @@ static mrb_value
...
@@ -2513,8 +2487,8 @@ static mrb_value
mrb_str_bytes
(
mrb_state
*
mrb
,
mrb_value
str
)
mrb_str_bytes
(
mrb_state
*
mrb
,
mrb_value
str
)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
mrb_value
a
=
mrb_ary_new_capa
(
mrb
,
STR_LEN
(
s
));
mrb_value
a
=
mrb_ary_new_capa
(
mrb
,
R
STR_LEN
(
s
));
unsigned
char
*
p
=
(
unsigned
char
*
)(
STR_PTR
(
s
)),
*
pend
=
p
+
STR_LEN
(
s
);
unsigned
char
*
p
=
(
unsigned
char
*
)(
RSTR_PTR
(
s
)),
*
pend
=
p
+
R
STR_LEN
(
s
);
while
(
p
<
pend
)
{
while
(
p
<
pend
)
{
mrb_ary_push
(
mrb
,
a
,
mrb_fixnum_value
(
p
[
0
]));
mrb_ary_push
(
mrb
,
a
,
mrb_fixnum_value
(
p
[
0
]));
...
@@ -2537,7 +2511,7 @@ mrb_str_clear(mrb_state *mrb, mrb_value str)
...
@@ -2537,7 +2511,7 @@ mrb_str_clear(mrb_state *mrb, mrb_value str)
{
{
struct
RString
*
s
=
mrb_str_ptr
(
str
);
struct
RString
*
s
=
mrb_str_ptr
(
str
);
if
(
!
STR_SHARED_P
(
s
)
&&
!
STR_EMBED_P
(
s
))
{
if
(
!
RSTR_SHARED_P
(
s
)
&&
!
R
STR_EMBED_P
(
s
))
{
if
(
s
->
flags
&
MRB_STR_NOFREE
)
{
if
(
s
->
flags
&
MRB_STR_NOFREE
)
{
s
->
flags
&=
~
MRB_STR_NOFREE
;
s
->
flags
&=
~
MRB_STR_NOFREE
;
}
}
...
@@ -2547,9 +2521,9 @@ mrb_str_clear(mrb_state *mrb, mrb_value str)
...
@@ -2547,9 +2521,9 @@ mrb_str_clear(mrb_state *mrb, mrb_value str)
s
->
as
.
heap
.
ptr
=
0
;
s
->
as
.
heap
.
ptr
=
0
;
s
->
as
.
heap
.
len
=
0
;
s
->
as
.
heap
.
len
=
0
;
}
}
STR_UNSET_SHARED_FLAG
(
s
);
R
STR_UNSET_SHARED_FLAG
(
s
);
STR_SET_EMBED_FLAG
(
s
);
R
STR_SET_EMBED_FLAG
(
s
);
STR_SET_EMBED_LEN
(
s
,
0
);
R
STR_SET_EMBED_LEN
(
s
,
0
);
RSTRING_PTR
(
str
)[
0
]
=
'\0'
;
RSTRING_PTR
(
str
)[
0
]
=
'\0'
;
return
str
;
return
str
;
}
}
...
...
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