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
3241a7e5
Commit
3241a7e5
authored
Nov 18, 2019
by
KOBAYASHI Shuji
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor `mrb_string_value_cstr`
- Keep `MRB_STR_ASCII` flag. - Avoid a string object creation.
parent
bd877996
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
26 deletions
+29
-26
src/string.c
src/string.c
+29
-26
No files found.
src/string.c
View file @
3241a7e5
...
@@ -245,6 +245,28 @@ str_decref(mrb_state *mrb, mrb_shared_string *shared)
...
@@ -245,6 +245,28 @@ str_decref(mrb_state *mrb, mrb_shared_string *shared)
}
}
}
}
static
void
str_modify_keep_ascii
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
if
(
RSTR_SHARED_P
(
s
))
{
mrb_shared_string
*
shared
=
s
->
as
.
heap
.
aux
.
shared
;
if
(
shared
->
refcnt
==
1
&&
s
->
as
.
heap
.
ptr
==
shared
->
ptr
)
{
s
->
as
.
heap
.
aux
.
capa
=
shared
->
capa
;
s
->
as
.
heap
.
ptr
[
s
->
as
.
heap
.
len
]
=
'\0'
;
RSTR_UNSET_SHARED_FLAG
(
s
);
mrb_free
(
mrb
,
shared
);
}
else
{
str_init_modifiable
(
mrb
,
s
,
s
->
as
.
heap
.
ptr
,
(
size_t
)
s
->
as
.
heap
.
len
);
str_decref
(
mrb
,
shared
);
}
}
else
if
(
RSTR_NOFREE_P
(
s
)
||
RSTR_FSHARED_P
(
s
))
{
str_init_modifiable
(
mrb
,
s
,
s
->
as
.
heap
.
ptr
,
(
size_t
)
s
->
as
.
heap
.
len
);
}
}
static
void
static
void
check_null_byte
(
mrb_state
*
mrb
,
mrb_value
str
)
check_null_byte
(
mrb_state
*
mrb
,
mrb_value
str
)
{
{
...
@@ -814,23 +836,7 @@ MRB_API void
...
@@ -814,23 +836,7 @@ MRB_API void
mrb_str_modify_keep_ascii
(
mrb_state
*
mrb
,
struct
RString
*
s
)
mrb_str_modify_keep_ascii
(
mrb_state
*
mrb
,
struct
RString
*
s
)
{
{
mrb_check_frozen
(
mrb
,
s
);
mrb_check_frozen
(
mrb
,
s
);
if
(
RSTR_SHARED_P
(
s
))
{
str_modify_keep_ascii
(
mrb
,
s
);
mrb_shared_string
*
shared
=
s
->
as
.
heap
.
aux
.
shared
;
if
(
shared
->
refcnt
==
1
&&
s
->
as
.
heap
.
ptr
==
shared
->
ptr
)
{
s
->
as
.
heap
.
aux
.
capa
=
shared
->
capa
;
s
->
as
.
heap
.
ptr
[
s
->
as
.
heap
.
len
]
=
'\0'
;
RSTR_UNSET_SHARED_FLAG
(
s
);
mrb_free
(
mrb
,
shared
);
}
else
{
str_init_modifiable
(
mrb
,
s
,
s
->
as
.
heap
.
ptr
,
(
size_t
)
s
->
as
.
heap
.
len
);
str_decref
(
mrb
,
shared
);
}
}
else
if
(
RSTR_NOFREE_P
(
s
)
||
RSTR_FSHARED_P
(
s
))
{
str_init_modifiable
(
mrb
,
s
,
s
->
as
.
heap
.
ptr
,
(
size_t
)
s
->
as
.
heap
.
len
);
}
}
}
MRB_API
void
MRB_API
void
...
@@ -2423,15 +2429,12 @@ mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr)
...
@@ -2423,15 +2429,12 @@ mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr)
if
(
p
[
len
]
==
'\0'
)
{
if
(
p
[
len
]
==
'\0'
)
{
return
p
;
return
p
;
}
}
if
(
mrb_frozen_p
(
ps
)
||
RSTR_CAPA
(
ps
)
==
len
)
{
ps
=
str_new
(
mrb
,
NULL
,
len
+
1
);
/*
memcpy
(
RSTR_PTR
(
ps
),
p
,
len
);
* Even after str_modify_keep_ascii(), NULL termination is not ensured if
RSTR_SET_LEN
(
ps
,
len
);
* RSTR_SET_LEN() is used explicitly (e.g. String#delete_suffix!).
*
ptr
=
mrb_obj_value
(
ps
);
*/
}
str_modify_keep_ascii
(
mrb
,
ps
);
else
{
mrb_str_modify
(
mrb
,
ps
);
}
RSTR_PTR
(
ps
)[
len
]
=
'\0'
;
RSTR_PTR
(
ps
)[
len
]
=
'\0'
;
return
RSTR_PTR
(
ps
);
return
RSTR_PTR
(
ps
);
}
}
...
...
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