Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
0b7e29c0
Commit
0b7e29c0
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
Factor out quoted string print function for reuse (mp_str_print_quoted()).
parent
8e991e06
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
py/obj.h
+1
-0
1 addition, 0 deletions
py/obj.h
py/objstr.c
+35
-31
35 additions, 31 deletions
py/objstr.c
with
36 additions
and
31 deletions
py/obj.h
+
1
−
0
View file @
0b7e29c0
...
...
@@ -291,6 +291,7 @@ uint mp_obj_str_get_len(mp_obj_t self_in);
qstr
mp_obj_str_get_qstr
(
mp_obj_t
self_in
);
// use this if you will anyway convert the string to a qstr
const
char
*
mp_obj_str_get_str
(
mp_obj_t
self_in
);
// use this only if you need the string to be null terminated
const
byte
*
mp_obj_str_get_data
(
mp_obj_t
self_in
,
uint
*
len
);
void
mp_str_print_quoted
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
const
byte
*
str_data
,
uint
str_len
);
// bytes
extern
const
mp_obj_type_t
bytes_type
;
...
...
This diff is collapsed.
Click to expand it.
py/objstr.c
+
35
−
31
View file @
0b7e29c0
...
...
@@ -34,45 +34,49 @@ static mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str);
/******************************************************************************/
/* str */
void
str_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
void
mp_str_print_quoted
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
const
byte
*
str_data
,
uint
str_len
)
{
// this escapes characters, but it will be very slow to print (calling print many times)
bool
has_single_quote
=
false
;
bool
has_double_quote
=
false
;
for
(
const
byte
*
s
=
str_data
,
*
top
=
str_data
+
str_len
;
(
!
has_single_quote
||
!
has_double_quote
)
&&
s
<
top
;
s
++
)
{
if
(
*
s
==
'\''
)
{
has_single_quote
=
true
;
}
else
if
(
*
s
==
'"'
)
{
has_double_quote
=
true
;
}
}
int
quote_char
=
'\''
;
if
(
has_single_quote
&&
!
has_double_quote
)
{
quote_char
=
'"'
;
}
print
(
env
,
"%c"
,
quote_char
);
for
(
const
byte
*
s
=
str_data
,
*
top
=
str_data
+
str_len
;
s
<
top
;
s
++
)
{
if
(
*
s
==
quote_char
)
{
print
(
env
,
"
\\
%c"
,
quote_char
);
}
else
if
(
*
s
==
'\\'
)
{
print
(
env
,
"
\\\\
"
);
}
else
if
(
32
<=
*
s
&&
*
s
<=
126
)
{
print
(
env
,
"%c"
,
*
s
);
}
else
if
(
*
s
==
'\n'
)
{
print
(
env
,
"
\\
n"
);
// TODO add more escape codes here if we want to match CPython
}
else
{
print
(
env
,
"
\\
x%02x"
,
*
s
);
}
}
print
(
env
,
"%c"
,
quote_char
);
}
static
void
str_print
(
void
(
*
print
)(
void
*
env
,
const
char
*
fmt
,
...),
void
*
env
,
mp_obj_t
self_in
,
mp_print_kind_t
kind
)
{
GET_STR_DATA_LEN
(
self_in
,
str_data
,
str_len
);
bool
is_bytes
=
MP_OBJ_IS_TYPE
(
self_in
,
&
bytes_type
);
if
(
kind
==
PRINT_STR
&&
!
is_bytes
)
{
print
(
env
,
"%.*s"
,
str_len
,
str_data
);
}
else
{
// this escapes characters, but it will be very slow to print (calling print many times)
bool
has_single_quote
=
false
;
bool
has_double_quote
=
false
;
for
(
const
byte
*
s
=
str_data
,
*
top
=
str_data
+
str_len
;
(
!
has_single_quote
||
!
has_double_quote
)
&&
s
<
top
;
s
++
)
{
if
(
*
s
==
'\''
)
{
has_single_quote
=
true
;
}
else
if
(
*
s
==
'"'
)
{
has_double_quote
=
true
;
}
}
if
(
is_bytes
)
{
print
(
env
,
"b"
);
}
int
quote_char
=
'\''
;
if
(
has_single_quote
&&
!
has_double_quote
)
{
quote_char
=
'"'
;
}
print
(
env
,
"%c"
,
quote_char
);
for
(
const
byte
*
s
=
str_data
,
*
top
=
str_data
+
str_len
;
s
<
top
;
s
++
)
{
if
(
*
s
==
quote_char
)
{
print
(
env
,
"
\\
%c"
,
quote_char
);
}
else
if
(
*
s
==
'\\'
)
{
print
(
env
,
"
\\\\
"
);
}
else
if
(
32
<=
*
s
&&
*
s
<=
126
)
{
print
(
env
,
"%c"
,
*
s
);
}
else
if
(
*
s
==
'\n'
)
{
print
(
env
,
"
\\
n"
);
// TODO add more escape codes here if we want to match CPython
}
else
{
print
(
env
,
"
\\
x%02x"
,
*
s
);
}
}
print
(
env
,
"%c"
,
quote_char
);
mp_str_print_quoted
(
print
,
env
,
str_data
,
str_len
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment