-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Added Ascii encoding, some cleanups #5980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
276293a
4357cbf
61ffee7
7ca216d
df61ec2
582a05f
bf4f088
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
|
@@ -17,6 +17,12 @@ | |
* some heavy-duty uses, try std::rope. | ||
*/ | ||
|
||
// NOTE: Remove markers after snapshot | ||
#[cfg(stage1)] | ||
#[cfg(stage2)] | ||
#[cfg(stage3)] | ||
pub use self::ascii::{Ascii, AsciiCast, OwnedAsciiCast, ToStrAscii}; | ||
|
||
use at_vec; | ||
use cast; | ||
use char; | ||
|
@@ -34,6 +40,13 @@ use to_str::ToStr; | |
|
||
#[cfg(notest)] use cmp::{Eq, Ord, Equiv, TotalEq}; | ||
|
||
// NOTE: Remove markers after snapshot | ||
#[cfg(stage1)] | ||
#[cfg(stage2)] | ||
#[cfg(stage3)] | ||
#[path = "str/ascii.rs"] | ||
mod ascii; | ||
|
||
/* | ||
Section: Creating a string | ||
*/ | ||
|
@@ -789,16 +802,18 @@ pub fn each_split_within<'a>(ss: &'a str, | |
|
||
/// Convert a string to lowercase. ASCII only | ||
pub fn to_lower(s: &str) -> ~str { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should just be removed since they're possible already by converting to Functions named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, wanted to make sure the the PR gets in first before starting to remove those functions, because that will surely touch some more code. |
||
map(s, | ||
|c| unsafe{(libc::tolower(c as libc::c_char)) as char} | ||
) | ||
do map(s) |c| { | ||
assert!(char::is_ascii(c)); | ||
(unsafe{libc::tolower(c as libc::c_char)}) as char | ||
} | ||
} | ||
|
||
/// Convert a string to uppercase. ASCII only | ||
pub fn to_upper(s: &str) -> ~str { | ||
map(s, | ||
|c| unsafe{(libc::toupper(c as libc::c_char)) as char} | ||
) | ||
do map(s) |c| { | ||
assert!(char::is_ascii(c)); | ||
(unsafe{libc::toupper(c as libc::c_char)}) as char | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -2317,20 +2332,20 @@ pub mod raw { | |
} | ||
|
||
/// Removes the last byte from a string and returns it. (Not UTF-8 safe). | ||
pub fn pop_byte(s: &mut ~str) -> u8 { | ||
pub unsafe fn pop_byte(s: &mut ~str) -> u8 { | ||
let len = len(*s); | ||
assert!((len > 0u)); | ||
let b = s[len - 1u]; | ||
unsafe { set_len(s, len - 1u) }; | ||
set_len(s, len - 1u); | ||
return b; | ||
} | ||
|
||
/// Removes the first byte from a string and returns it. (Not UTF-8 safe). | ||
pub fn shift_byte(s: &mut ~str) -> u8 { | ||
pub unsafe fn shift_byte(s: &mut ~str) -> u8 { | ||
let len = len(*s); | ||
assert!((len > 0u)); | ||
let b = s[0]; | ||
*s = unsafe { raw::slice_bytes_owned(*s, 1u, len) }; | ||
*s = raw::slice_bytes_owned(*s, 1u, len); | ||
return b; | ||
} | ||
|
||
|
@@ -3096,12 +3111,11 @@ mod tests { | |
|
||
#[test] | ||
fn test_to_lower() { | ||
unsafe { | ||
assert!(~"" == map(~"", | ||
|c| libc::tolower(c as c_char) as char)); | ||
assert!(~"ymca" == map(~"YMCA", | ||
|c| libc::tolower(c as c_char) as char)); | ||
} | ||
// libc::tolower, and hence str::to_lower | ||
// are culturally insensitive: they only work for ASCII | ||
// (see Issue #1347) | ||
assert!(~"" == to_lower("")); | ||
assert!(~"ymca" == to_lower("YMCA")); | ||
} | ||
|
||
#[test] | ||
|
@@ -3346,15 +3360,15 @@ mod tests { | |
#[test] | ||
fn test_shift_byte() { | ||
let mut s = ~"ABC"; | ||
let b = raw::shift_byte(&mut s); | ||
let b = unsafe{raw::shift_byte(&mut s)}; | ||
assert!((s == ~"BC")); | ||
assert!((b == 65u8)); | ||
} | ||
|
||
#[test] | ||
fn test_pop_byte() { | ||
let mut s = ~"ABC"; | ||
let b = raw::pop_byte(&mut s); | ||
let b = unsafe{raw::pop_byte(&mut s)}; | ||
assert!((s == ~"AB")); | ||
assert!((b == 67u8)); | ||
} | ||
|
@@ -3666,12 +3680,8 @@ mod tests { | |
|
||
#[test] | ||
fn test_map() { | ||
unsafe { | ||
assert!(~"" == map(~"", |c| | ||
libc::toupper(c as c_char) as char)); | ||
assert!(~"YMCA" == map(~"ymca", | ||
|c| libc::toupper(c as c_char) as char)); | ||
} | ||
assert!(~"" == map(~"", |c| unsafe {libc::toupper(c as c_char)} as char)); | ||
assert!(~"YMCA" == map(~"ymca", |c| unsafe {libc::toupper(c as c_char)} as char)); | ||
} | ||
|
||
#[test] | ||
|
@@ -3685,11 +3695,11 @@ mod tests { | |
|
||
#[test] | ||
fn test_any() { | ||
assert!(false == any(~"", char::is_uppercase)); | ||
assert!(false == any(~"", char::is_uppercase)); | ||
assert!(false == any(~"ymca", char::is_uppercase)); | ||
assert!(true == any(~"YMCA", char::is_uppercase)); | ||
assert!(true == any(~"yMCA", char::is_uppercase)); | ||
assert!(true == any(~"Ymcy", char::is_uppercase)); | ||
assert!(true == any(~"yMCA", char::is_uppercase)); | ||
assert!(true == any(~"Ymcy", char::is_uppercase)); | ||
} | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be a sub-module of
str
, since it's only related to it in the same way it is to[u8]
(conversion). Perhaps a top-level module in libstd?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, could put it in a separate top-level module, It's globally imported in the prelude anyway. I'd be in favor of having at least ascii in core though (other byte encodings, codepages etc I'd put in std, though)