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
be647acd
Unverified
Commit
be647acd
authored
May 30, 2021
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sprintf.c: avoid object allocation in integer formatting.
parent
8a4bcc58
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
38 deletions
+16
-38
mrbgems/mruby-sprintf/src/sprintf.c
mrbgems/mruby-sprintf/src/sprintf.c
+16
-38
No files found.
mrbgems/mruby-sprintf/src/sprintf.c
View file @
be647acd
...
...
@@ -68,35 +68,24 @@ sign_bits(int base, const char *p)
return
c
;
}
static
mrb_value
mrb_
fix2binstr
(
mrb_state
*
mrb
,
mrb_value
x
,
int
base
)
static
char
*
mrb_
uint_to_cstr
(
char
*
buf
,
size_t
len
,
mrb_int
num
,
mrb_
int
base
)
{
char
buf
[
66
],
*
b
=
buf
+
sizeof
buf
;
mrb_int
num
=
mrb_integer
(
x
);
const
int
mask
=
base
-
1
;
char
*
b
=
buf
+
len
-
1
;
const
int
mask
=
base
-
1
;
int
shift
;
#ifdef MRB_INT64
uint64_t
val
=
(
uint64_t
)
num
;
#else
uint32_t
val
=
(
uint32_t
)
num
;
#endif
mrb_uint
val
=
(
uint64_t
)
num
;
char
d
;
switch
(
base
)
{
case
2
:
shift
=
1
;
break
;
case
8
:
shift
=
3
;
break
;
case
16
:
shift
=
4
;
break
;
default:
mrb_raisef
(
mrb
,
E_ARGUMENT_ERROR
,
"invalid radix %d"
,
base
);
}
if
(
num
==
0
)
{
return
mrb_str_new_lit
(
mrb
,
"0"
);
buf
[
0
]
=
'0'
;
buf
[
1
]
=
'\0'
;
return
buf
;
}
switch
(
base
)
{
case
16
:
d
=
'f'
;
shift
=
4
;
break
;
case
8
:
d
=
'7'
;
shift
=
3
;
break
;
case
2
:
d
=
'1'
;
shift
=
1
;
break
;
default:
return
NULL
;
}
*--
b
=
'\0'
;
do
{
...
...
@@ -105,19 +94,12 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base)
if
(
num
<
0
)
{
b
=
remove_sign_bits
(
b
,
base
);
switch
(
base
)
{
case
16
:
d
=
'f'
;
break
;
case
8
:
d
=
'7'
;
break
;
case
2
:
d
=
'1'
;
break
;
default:
d
=
0
;
break
;
}
if
(
d
&&
*
b
!=
d
)
{
*--
b
=
d
;
}
}
return
mrb_str_new_cstr
(
mrb
,
b
)
;
return
b
;
}
#define FNONE 0
...
...
@@ -943,15 +925,11 @@ retry:
if
(
v
<
0
)
s
++
;
/* skip minus sign */
}
else
{
/* print as unsigned */
s
=
mrb_uint_to_cstr
(
nbuf
,
sizeof
(
nbuf
),
v
,
base
);
if
(
v
<
0
)
{
dots
=
1
;
val
=
mrb_fix2binstr
(
mrb
,
mrb_int_value
(
mrb
,
v
),
base
);
}
else
{
val
=
mrb_integer_to_str
(
mrb
,
mrb_int_value
(
mrb
,
v
),
base
);
}
strncpy
(
nbuf
+
1
,
RSTRING_PTR
(
val
),
sizeof
(
nbuf
)
-
2
);
s
=
nbuf
+
1
;
}
{
size_t
size
;
...
...
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