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
5fb5c2a4
Commit
5fb5c2a4
authored
Apr 29, 2014
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2155 from cremno/define-resize_capa-as-a-function
define `resize_capa` as a function
parents
8639703b
92632c61
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
19 deletions
+22
-19
src/string.c
src/string.c
+22
-19
No files found.
src/string.c
View file @
5fb5c2a4
...
...
@@ -70,22 +70,25 @@ mrb_str_strlen(mrb_state *mrb, struct RString *s)
return
max
;
}
#define RESIZE_CAPA(s,capacity) do {\
if (STR_EMBED_P(s)) {\
if (RSTRING_EMBED_LEN_MAX < (capacity)) {\
char *const __tmp__ = (char *)mrb_malloc(mrb, (capacity)+1);\
const mrb_int __len__ = STR_EMBED_LEN(s);\
memcpy(__tmp__, s->as.ary, __len__);\
STR_UNSET_EMBED_FLAG(s);\
s->as.heap.ptr = __tmp__;\
s->as.heap.len = __len__;\
s->as.heap.aux.capa = (capacity);\
}\
} else {\
s->as.heap.ptr = (char *)mrb_realloc(mrb, STR_PTR(s), (capacity)+1);\
s->as.heap.aux.capa = capacity;\
}\
} while(0)
static
inline
void
resize_capa
(
mrb_state
*
mrb
,
struct
RString
*
s
,
mrb_int
capacity
)
{
if
(
STR_EMBED_P
(
s
))
{
if
(
RSTRING_EMBED_LEN_MAX
<
capacity
)
{
char
*
const
tmp
=
(
char
*
)
mrb_malloc
(
mrb
,
capacity
+
1
);
const
mrb_int
len
=
STR_EMBED_LEN
(
s
);
memcpy
(
tmp
,
s
->
as
.
ary
,
len
);
STR_UNSET_EMBED_FLAG
(
s
);
s
->
as
.
heap
.
ptr
=
tmp
;
s
->
as
.
heap
.
len
=
len
;
s
->
as
.
heap
.
aux
.
capa
=
capacity
;
}
}
else
{
s
->
as
.
heap
.
ptr
=
(
char
*
)
mrb_realloc
(
mrb
,
STR_PTR
(
s
),
capacity
+
1
);
s
->
as
.
heap
.
aux
.
capa
=
capacity
;
}
}
static
void
str_decref
(
mrb_state
*
mrb
,
mrb_shared_string
*
shared
)
...
...
@@ -153,7 +156,7 @@ mrb_str_resize(mrb_state *mrb, mrb_value str, mrb_int len)
slen
=
STR_LEN
(
s
);
if
(
len
!=
slen
)
{
if
(
slen
<
len
||
slen
-
len
>
256
)
{
RESIZE_CAPA
(
s
,
len
);
resize_capa
(
mrb
,
s
,
len
);
}
STR_SET_LEN
(
s
,
len
);
STR_PTR
(
s
)[
len
]
=
'\0'
;
/* sentinel */
...
...
@@ -260,7 +263,7 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len)
}
capa
=
(
capa
+
1
)
*
2
;
}
RESIZE_CAPA
(
s
,
capa
);
resize_capa
(
mrb
,
s
,
capa
);
}
if
(
off
!=
-
1
)
{
ptr
=
STR_PTR
(
s
)
+
off
;
...
...
@@ -421,7 +424,7 @@ mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other)
len
=
STR_LEN
(
s1
)
+
STR_LEN
(
s2
);
if
(
RSTRING_CAPA
(
self
)
<
len
)
{
RESIZE_CAPA
(
s1
,
len
);
resize_capa
(
mrb
,
s1
,
len
);
}
memcpy
(
STR_PTR
(
s1
)
+
STR_LEN
(
s1
),
STR_PTR
(
s2
),
STR_LEN
(
s2
));
STR_SET_LEN
(
s1
,
len
);
...
...
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