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
87e85b7d
Commit
87e85b7d
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
Implement str/bytes rich comparisons.
parent
9ed54350
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/obj.h
+1
-0
1 addition, 0 deletions
py/obj.h
py/objstr.c
+12
-0
12 additions, 0 deletions
py/objstr.c
py/sequence.c
+38
-0
38 additions, 0 deletions
py/sequence.c
tests/basics/string-compare.py
+50
-0
50 additions, 0 deletions
tests/basics/string-compare.py
with
101 additions
and
0 deletions
py/obj.h
+
1
−
0
View file @
87e85b7d
...
...
@@ -397,3 +397,4 @@ typedef struct _mp_obj_classmethod_t {
void
mp_seq_multiply
(
const
void
*
items
,
uint
item_sz
,
uint
len
,
uint
times
,
void
*
dest
);
bool
m_seq_get_fast_slice_indexes
(
machine_uint_t
len
,
mp_obj_t
slice
,
machine_uint_t
*
begin
,
machine_uint_t
*
end
);
#define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz))
bool
mp_seq_cmp_bytes
(
int
op
,
const
byte
*
data1
,
uint
len1
,
const
byte
*
data2
,
uint
len2
);
This diff is collapsed.
Click to expand it.
py/objstr.c
+
12
−
0
View file @
87e85b7d
...
...
@@ -169,6 +169,18 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
mp_seq_multiply
(
lhs_data
,
sizeof
(
*
lhs_data
),
lhs_len
,
n
,
data
);
return
mp_obj_str_builder_end
(
s
);
}
// These 2 are never passed here, dealt with as a special case in rt_binary_op().
//case RT_BINARY_OP_EQUAL:
//case RT_BINARY_OP_NOT_EQUAL:
case
RT_BINARY_OP_LESS
:
case
RT_BINARY_OP_LESS_EQUAL
:
case
RT_BINARY_OP_MORE
:
case
RT_BINARY_OP_MORE_EQUAL
:
if
(
MP_OBJ_IS_STR
(
rhs_in
))
{
GET_STR_DATA_LEN
(
rhs_in
,
rhs_data
,
rhs_len
);
return
MP_BOOL
(
mp_seq_cmp_bytes
(
op
,
lhs_data
,
lhs_len
,
rhs_data
,
rhs_len
));
}
}
return
MP_OBJ_NULL
;
// op not supported
...
...
This diff is collapsed.
Click to expand it.
py/sequence.c
+
38
−
0
View file @
87e85b7d
...
...
@@ -14,6 +14,8 @@
// Helpers for sequence types
#define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
// Implements backend of sequence * integer operation. Assumes elements are
// memory-adjacent in sequence.
void
mp_seq_multiply
(
const
void
*
items
,
uint
item_sz
,
uint
len
,
uint
times
,
void
*
dest
)
{
...
...
@@ -53,3 +55,39 @@ bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_ui
*
end
=
stop
;
return
true
;
}
// Special-case comparison function for sequences of bytes
// Don't pass RT_BINARY_OP_NOT_EQUAL here
bool
mp_seq_cmp_bytes
(
int
op
,
const
byte
*
data1
,
uint
len1
,
const
byte
*
data2
,
uint
len2
)
{
// Let's deal only with > & >=
if
(
op
==
RT_BINARY_OP_LESS
||
op
==
RT_BINARY_OP_LESS_EQUAL
)
{
SWAP
(
const
byte
*
,
data1
,
data2
);
SWAP
(
uint
,
len1
,
len2
);
if
(
op
==
RT_BINARY_OP_LESS
)
{
op
=
RT_BINARY_OP_MORE
;
}
else
{
op
=
RT_BINARY_OP_MORE_EQUAL
;
}
}
uint
min_len
=
len1
<
len2
?
len1
:
len2
;
int
res
=
memcmp
(
data1
,
data2
,
min_len
);
if
(
res
<
0
)
{
return
false
;
}
if
(
res
>
0
)
{
return
true
;
}
// If we had tie in the last element...
// ... and we have lists of different lengths...
if
(
len1
!=
len2
)
{
if
(
len1
<
len2
)
{
// ... then longer list length wins (we deal only with >)
return
false
;
}
}
else
if
(
op
==
RT_BINARY_OP_MORE
)
{
// Otherwise, if we have strict relation, equality means failure
return
false
;
}
return
true
;
}
This diff is collapsed.
Click to expand it.
tests/basics/string-compare.py
0 → 100644
+
50
−
0
View file @
87e85b7d
print
(
""
==
""
)
print
(
""
>
""
)
print
(
""
<
""
)
print
(
""
==
"
1
"
)
print
(
"
1
"
==
""
)
print
(
""
>
"
1
"
)
print
(
"
1
"
>
""
)
print
(
""
<
"
1
"
)
print
(
"
1
"
<
""
)
print
(
""
>=
"
1
"
)
print
(
"
1
"
>=
""
)
print
(
""
<=
"
1
"
)
print
(
"
1
"
<=
""
)
print
(
"
1
"
==
"
1
"
)
print
(
"
1
"
!=
"
1
"
)
print
(
"
1
"
==
"
2
"
)
print
(
"
1
"
==
"
10
"
)
print
(
"
1
"
>
"
1
"
)
print
(
"
1
"
>
"
2
"
)
print
(
"
2
"
>
"
1
"
)
print
(
"
10
"
>
"
1
"
)
print
(
"
1/
"
>
"
1
"
)
print
(
"
1
"
>
"
10
"
)
print
(
"
1
"
>
"
1/
"
)
print
(
"
1
"
<
"
1
"
)
print
(
"
2
"
<
"
1
"
)
print
(
"
1
"
<
"
2
"
)
print
(
"
1
"
<
"
10
"
)
print
(
"
1
"
<
"
1/
"
)
print
(
"
10
"
<
"
1
"
)
print
(
"
1/
"
<
"
1
"
)
print
(
"
1
"
>=
"
1
"
)
print
(
"
1
"
>=
"
2
"
)
print
(
"
2
"
>=
"
1
"
)
print
(
"
10
"
>=
"
1
"
)
print
(
"
1/
"
>=
"
1
"
)
print
(
"
1
"
>=
"
10
"
)
print
(
"
1
"
>=
"
1/
"
)
print
(
"
1
"
<=
"
1
"
)
print
(
"
2
"
<=
"
1
"
)
print
(
"
1
"
<=
"
2
"
)
print
(
"
1
"
<=
"
10
"
)
print
(
"
1
"
<=
"
1/
"
)
print
(
"
10
"
<=
"
1
"
)
print
(
"
1/
"
<=
"
1
"
)
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