Skip to content

Commit 392d3c8

Browse files
committed
core: Add extension methods for str
1 parent 35a3fa0 commit 392d3c8

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/libcore/core.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import option::{some, none};
66
import option = option::option;
77
import path = path::path;
88
import vec::vec_len;
9+
import str::extensions;
910
export path, option, some, none, vec_len, unreachable;
11+
export extensions;
1012

1113
// Export the log levels as global constants. Higher levels mean
1214
// more-verbosity. Error is the bottom level, default logging level is

src/libcore/str.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export
9595
reserve_at_least,
9696
capacity,
9797

98-
unsafe;
98+
unsafe,
99+
extensions;
99100

100101
#[abi = "cdecl"]
101102
native mod rustrt {
@@ -1730,6 +1731,88 @@ mod unsafe {
17301731
}
17311732
}
17321733

1734+
#[doc = "Extension methods for strings"]
1735+
impl extensions for str {
1736+
#[doc = "
1737+
Return true if a predicate matches all characters or if the string
1738+
contains no characters
1739+
"]
1740+
#[inline]
1741+
fn all(it: fn(char) -> bool) -> bool { all(self, it) }
1742+
#[doc = "
1743+
Return true if a predicate matches any character (and false if it
1744+
matches none or there are no characters)
1745+
"]
1746+
#[inline]
1747+
fn any(it: fn(char) -> bool) -> bool { any(self, it) }
1748+
#[doc = "Returns true if one string contains another"]
1749+
#[inline]
1750+
fn contains(needle: str) -> bool { contains(self, needle) }
1751+
#[doc = "Returns true if one string ends with another"]
1752+
#[inline]
1753+
fn ends_with(needle: str) -> bool { ends_with(self, needle) }
1754+
#[doc = "Returns true if the string has length 0"]
1755+
#[inline]
1756+
fn is_empty() -> bool { is_empty(self) }
1757+
#[doc = "Returns true if the string has length greater than 0"]
1758+
#[inline]
1759+
fn is_not_empty() -> bool { is_not_empty(self) }
1760+
#[doc = "
1761+
Returns true if the string contains only whitespace
1762+
1763+
Whitespace characters are determined by `char::is_whitespace`
1764+
"]
1765+
#[inline]
1766+
fn is_whitespace() -> bool { is_whitespace(self) }
1767+
#[doc = "
1768+
Returns a slice of the given string from the byte range [`begin`..`end`)
1769+
1770+
Fails when `begin` and `end` do not point to valid characters or
1771+
beyond the last character of the string
1772+
"]
1773+
#[inline]
1774+
fn slice(begin: uint, end: uint) -> str { slice(self, begin, end) }
1775+
#[doc = "Splits a string into substrings using a character function"]
1776+
#[inline]
1777+
fn split(sepfn: fn(char) -> bool) -> [str] { split(self, sepfn) }
1778+
#[doc = "
1779+
Splits a string into substrings at each occurrence of a given character
1780+
"]
1781+
#[inline]
1782+
fn split_char(sep: char) -> [str] { split_char(self, sep) }
1783+
#[doc = "
1784+
Splits a string into a vector of the substrings separated by a given
1785+
string
1786+
"]
1787+
#[inline]
1788+
fn split_str(sep: str) -> [str] { split_str(self, sep) }
1789+
#[doc = "Returns true if one string starts with another"]
1790+
#[inline]
1791+
fn starts_with(needle: str) -> bool { starts_with(self, needle) }
1792+
#[doc = "
1793+
Take a substring of another.
1794+
1795+
Returns a string containing `n` characters starting at byte offset
1796+
`begin`.
1797+
"]
1798+
#[inline]
1799+
fn substr(begin: uint, n: uint) -> str { substr(self, begin, n) }
1800+
#[doc = "Convert a string to lowercase"]
1801+
#[inline]
1802+
fn to_lower() -> str { to_lower(self) }
1803+
#[doc = "Convert a string to uppercase"]
1804+
#[inline]
1805+
fn to_upper() -> str { to_upper(self) }
1806+
#[doc = "Returns a string with leading and trailing whitespace removed"]
1807+
#[inline]
1808+
fn trim() -> str { trim(self) }
1809+
#[doc = "Returns a string with leading whitespace removed"]
1810+
#[inline]
1811+
fn trim_left() -> str { trim_left(self) }
1812+
#[doc = "Returns a string with trailing whitespace removed"]
1813+
#[inline]
1814+
fn trim_right() -> str { trim_right(self) }
1815+
}
17331816

17341817
#[cfg(test)]
17351818
mod tests {

0 commit comments

Comments
 (0)