Skip to content
Snippets Groups Projects
Unverified Commit 045a510c authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub
Browse files

Merge pull request #92 from tdbgamer/feature/strtok

Implement strtok
parents ca61b4cb 3a89f66c
No related branches found
No related tags found
No related merge requests found
/target/
.idea/
......@@ -323,17 +323,49 @@ pub unsafe extern "C" fn strstr(s1: *const c_char, s2: *const c_char) -> *mut c_
}
#[no_mangle]
pub extern "C" fn strtok(s1: *mut c_char, s2: *const c_char) -> *mut c_char {
unimplemented!();
pub extern "C" fn strtok(s1: *mut c_char, delimiter: *const c_char) -> *mut c_char {
static mut HAYSTACK: *mut c_char = ptr::null_mut();
unsafe {
return strtok_r(s1, delimiter, &mut HAYSTACK);
}
}
#[no_mangle]
pub extern "C" fn strtok_r(
s: *mut c_char,
sep: *const c_char,
delimiter: *const c_char,
lasts: *mut *mut c_char,
) -> *mut c_char {
unimplemented!();
// Loosely based on GLIBC implementation
unsafe {
let mut haystack = s;
if haystack.is_null() {
if (*lasts).is_null() {
return ptr::null_mut();
}
haystack = *lasts;
}
// Skip past any extra delimiter left over from previous call
haystack = haystack.add(strspn(haystack, delimiter));
if *haystack == 0 {
*lasts = ptr::null_mut();
return ptr::null_mut();
}
// Build token by injecting null byte into delimiter
let token = haystack;
haystack = strpbrk(token, delimiter);
if !haystack.is_null() {
haystack.write(0);
haystack = haystack.add(1);
*lasts = haystack;
} else {
*lasts = ptr::null_mut();
}
return token;
}
}
#[no_mangle]
......
......@@ -30,6 +30,8 @@ EXPECT_BINS=\
string/strspn \
string/strstr \
string/strpbrk \
string/strtok \
string/strtok_r \
unlink \
waitpid \
write
......
I'd_just_like_to_interject_for_a_moment.__What_you're_referring_to_as_Linux,
is_in_fact,_GNU/Linux,_or_as_I've_recently_taken_to_calling_it,_GNU_plus_Linux.
I'd_just_like_to_interject_for_a_moment.__What_you're_referring_to_as_Linux,
is_in_fact,_GNU/Linux,_or_as_I've_recently_taken_to_calling_it,_GNU_plus_Linux.
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char source[] = "I'd just like to interject for a moment. What you're referring to as Linux, "
"is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.\n";
char* token = strtok(source, " ");
while (token) {
printf("%s", token);
if (token = strtok(NULL, " ")) {
printf("_");
}
}
return 0;
}
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char source[] = "I'd just like to interject for a moment. What you're referring to as Linux, "
"is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.\n";
char* sp;
char* token = strtok_r(source, " ", &sp);
while (token) {
printf("%s", token);
if (token = strtok_r(NULL, " ", &sp)) {
printf("_");
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment