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
GitLab 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
9a1a4beb
Commit
9a1a4beb
authored
11 years ago
by
Chris Angelico
Committed by
Paul Sokolovsky
11 years ago
Browse files
Options
Downloads
Patches
Plain Diff
builtin: ord, chr: Unicode support.
parent
64b468d8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
py/builtin.c
+36
-9
36 additions, 9 deletions
py/builtin.c
with
36 additions
and
9 deletions
py/builtin.c
+
36
−
9
View file @
9a1a4beb
...
@@ -172,13 +172,30 @@ STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
...
@@ -172,13 +172,30 @@ STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_callable_obj
,
mp_builtin_callable
);
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_callable_obj
,
mp_builtin_callable
);
STATIC
mp_obj_t
mp_builtin_chr
(
mp_obj_t
o_in
)
{
STATIC
mp_obj_t
mp_builtin_chr
(
mp_obj_t
o_in
)
{
int
ord
=
mp_obj_get_int
(
o_in
);
int
c
=
mp_obj_get_int
(
o_in
);
if
(
0
<=
ord
&&
ord
<=
0x10ffff
)
{
char
str
[
4
];
char
str
[
1
]
=
{
ord
};
int
len
=
0
;
return
mp_obj_new_str
(
str
,
1
,
true
);
if
(
c
<
0x80
)
{
*
str
=
c
;
len
=
1
;
}
else
if
(
c
<
0x800
)
{
str
[
0
]
=
(
c
>>
6
)
|
0xC0
;
str
[
1
]
=
(
c
&
0x3F
)
|
0x80
;
len
=
2
;
}
else
if
(
c
<
0x10000
)
{
str
[
0
]
=
(
c
>>
12
)
|
0xE0
;
str
[
1
]
=
((
c
>>
6
)
&
0x3F
)
|
0x80
;
str
[
2
]
=
(
c
&
0x3F
)
|
0x80
;
len
=
3
;
}
else
if
(
c
<
0x110000
)
{
str
[
0
]
=
(
c
>>
18
)
|
0xF0
;
str
[
1
]
=
((
c
>>
12
)
&
0x3F
)
|
0x80
;
str
[
2
]
=
((
c
>>
6
)
&
0x3F
)
|
0x80
;
str
[
3
]
=
(
c
&
0x3F
)
|
0x80
;
len
=
4
;
}
else
{
}
else
{
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_ValueError
,
"chr() arg not in range(0x110000)"
));
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_ValueError
,
"chr() arg not in range(0x110000)"
));
}
}
return
mp_obj_new_str
(
str
,
len
,
true
);
}
}
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_chr_obj
,
mp_builtin_chr
);
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_chr_obj
,
mp_builtin_chr
);
...
@@ -344,12 +361,22 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
...
@@ -344,12 +361,22 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_oct_obj, mp_builtin_oct);
STATIC
mp_obj_t
mp_builtin_ord
(
mp_obj_t
o_in
)
{
STATIC
mp_obj_t
mp_builtin_ord
(
mp_obj_t
o_in
)
{
uint
len
;
uint
len
;
const
char
*
str
=
mp_obj_str_get_data
(
o_in
,
&
len
);
const
char
*
str
=
mp_obj_str_get_data
(
o_in
,
&
len
);
if
(
len
==
1
)
{
uint
charlen
=
unichar_charlen
(
str
,
len
);
// don't sign extend when converting to ord
if
(
charlen
==
1
)
{
// TODO unicode
if
(
MP_OBJ_IS_STR
(
o_in
)
&&
UTF8_IS_NONASCII
(
*
str
))
{
machine_int_t
ord
=
*
str
++
&
0x7F
;
for
(
machine_int_t
mask
=
0x40
;
ord
&
mask
;
mask
>>=
1
)
{
ord
&=
~
mask
;
}
while
(
UTF8_IS_CONT
(
*
str
))
{
ord
=
(
ord
<<
6
)
|
(
*
str
++
&
0x3F
);
}
return
mp_obj_new_int
(
ord
);
}
else
{
return
mp_obj_new_int
(((
const
byte
*
)
str
)[
0
]);
return
mp_obj_new_int
(((
const
byte
*
)
str
)[
0
]);
}
}
else
{
}
else
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"ord() expected a character, but string of length %d found"
,
len
));
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_TypeError
,
"ord() expected a character, but string of length %d found"
,
char
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