Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relibc
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
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
redox-os
relibc
Commits
b60fb9ba
Commit
b60fb9ba
authored
5 months ago
by
Peter Limkilde Svendsen
Committed by
Jeremy Soller
5 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Refactor strcasecmp, strncasecmp with iterators
parent
ff853a7b
No related branches found
No related tags found
1 merge request
!524
Refactor strcasecmp, strncasecmp with iterators
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/header/strings/mod.rs
+27
-18
27 additions, 18 deletions
src/header/strings/mod.rs
with
27 additions
and
18 deletions
src/header/strings/mod.rs
+
27
−
18
View file @
b60fb9ba
//! strings implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/strings.h.html
//! strings implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/strings.h.html
use
core
::{
arch
,
ptr
};
use
core
::{
arch
,
iter
::{
once
,
zip
},
ptr
,
};
use
crate
::{
use
crate
::{
header
::{
ctype
,
string
},
header
::{
ctype
,
string
},
iter
::
NulTerminated
,
platform
::
types
::
*
,
platform
::
types
::
*
,
};
};
...
@@ -49,25 +54,29 @@ pub unsafe extern "C" fn rindex(s: *const c_char, c: c_int) -> *mut c_char {
...
@@ -49,25 +54,29 @@ pub unsafe extern "C" fn rindex(s: *const c_char, c: c_int) -> *mut c_char {
}
}
#[no_mangle]
#[no_mangle]
pub
unsafe
extern
"C"
fn
strcasecmp
(
first
:
*
const
c_char
,
second
:
*
const
c_char
)
->
c_int
{
pub
unsafe
extern
"C"
fn
strcasecmp
(
s1
:
*
const
c_char
,
s2
:
*
const
c_char
)
->
c_int
{
strncasecmp
(
first
,
second
,
size_t
::
max_value
())
// SAFETY: the caller must ensure that s1 and s2 point to nul-terminated buffers.
let
s1_iter
=
unsafe
{
NulTerminated
::
new
(
s1
)
}
.chain
(
once
(
&
0
));
let
s2_iter
=
unsafe
{
NulTerminated
::
new
(
s2
)
}
.chain
(
once
(
&
0
));
let
zipped
=
zip
(
s1_iter
,
s2_iter
);
inner_casecmp
(
zipped
)
}
}
#[no_mangle]
#[no_mangle]
pub
unsafe
extern
"C"
fn
strncasecmp
(
pub
unsafe
extern
"C"
fn
strncasecmp
(
s1
:
*
const
c_char
,
s2
:
*
const
c_char
,
n
:
size_t
)
->
c_int
{
mut
first
:
*
const
c_char
,
// SAFETY: the caller must ensure that s1 and s2 point to nul-terminated buffers.
mut
second
:
*
const
c_char
,
let
s1_iter
=
unsafe
{
NulTerminated
::
new
(
s1
)
}
.chain
(
once
(
&
0
));
mut
n
:
size_t
,
let
s2_iter
=
unsafe
{
NulTerminated
::
new
(
s2
)
}
.chain
(
once
(
&
0
));
)
->
c_int
{
while
n
>
0
&&
(
*
first
!=
0
||
*
second
!=
0
)
{
let
cmp
=
ctype
::
tolower
(
*
first
as
c_int
)
-
ctype
::
tolower
(
*
second
as
c_int
);
if
cmp
!=
0
{
return
cmp
;
}
first
=
first
.offset
(
1
);
let
zipped
=
zip
(
s1_iter
,
s2_iter
)
.take
(
n
);
second
=
second
.offset
(
1
);
inner_casecmp
(
zipped
)
n
-=
1
;
}
}
0
/// Given two zipped `&c_char` iterators, either find the first comparison != 0, or return 0.
fn
inner_casecmp
<
'a
>
(
iterator
:
impl
Iterator
<
Item
=
(
&
'a
c_char
,
&
'a
c_char
)
>
)
->
c_int
{
let
mut
cmp_iter
=
iterator
.map
(|(
&
c1
,
&
c2
)|
ctype
::
tolower
(
c1
.into
())
-
ctype
::
tolower
(
c2
.into
()));
let
mut
skip_iter
=
cmp_iter
.skip_while
(|
&
cmp
|
cmp
==
0
);
skip_iter
.next
()
.or
(
Some
(
0
))
.unwrap
()
}
}
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