]> git.lizzy.rs Git - rust.git/blob - src/libcore/str.rs
b336c57efa07228ad8d6d54518b3e6af46904079
[rust.git] / src / libcore / str.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! String manipulation
12 //!
13 //! For more details, see std::str
14
15 #![doc(primitive = "str")]
16
17 use mem;
18 use char;
19 use char::Char;
20 use clone::Clone;
21 use cmp;
22 use cmp::{PartialEq, Eq};
23 use collections::Collection;
24 use default::Default;
25 use iter::{Filter, Map, Iterator};
26 use iter::{DoubleEndedIterator, ExactSize};
27 use iter::range;
28 use num::{CheckedMul, Saturating};
29 use option::{None, Option, Some};
30 use raw::Repr;
31 use slice::ImmutableVector;
32 use slice;
33 use uint;
34
35 /*
36 Section: Creating a string
37 */
38
39 /// Converts a vector to a string slice without performing any allocations.
40 ///
41 /// Once the slice has been validated as utf-8, it is transmuted in-place and
42 /// returned as a '&str' instead of a '&[u8]'
43 ///
44 /// Returns None if the slice is not utf-8.
45 pub fn from_utf8<'a>(v: &'a [u8]) -> Option<&'a str> {
46     if is_utf8(v) {
47         Some(unsafe { raw::from_utf8(v) })
48     } else { None }
49 }
50
51 /// Something that can be used to compare against a character
52 pub trait CharEq {
53     /// Determine if the splitter should split at the given character
54     fn matches(&mut self, char) -> bool;
55     /// Indicate if this is only concerned about ASCII characters,
56     /// which can allow for a faster implementation.
57     fn only_ascii(&self) -> bool;
58 }
59
60 impl CharEq for char {
61     #[inline]
62     fn matches(&mut self, c: char) -> bool { *self == c }
63
64     #[inline]
65     fn only_ascii(&self) -> bool { (*self as uint) < 128 }
66 }
67
68 impl<'a> CharEq for |char|: 'a -> bool {
69     #[inline]
70     fn matches(&mut self, c: char) -> bool { (*self)(c) }
71
72     #[inline]
73     fn only_ascii(&self) -> bool { false }
74 }
75
76 impl CharEq for extern "Rust" fn(char) -> bool {
77     #[inline]
78     fn matches(&mut self, c: char) -> bool { (*self)(c) }
79
80     #[inline]
81     fn only_ascii(&self) -> bool { false }
82 }
83
84 impl<'a> CharEq for &'a [char] {
85     #[inline]
86     fn matches(&mut self, c: char) -> bool {
87         self.iter().any(|&mut m| m.matches(c))
88     }
89
90     #[inline]
91     fn only_ascii(&self) -> bool {
92         self.iter().all(|m| m.only_ascii())
93     }
94 }
95
96 /*
97 Section: Iterators
98 */
99
100 /// External iterator for a string's characters.
101 /// Use with the `std::iter` module.
102 #[deriving(Clone)]
103 pub struct Chars<'a> {
104     /// The slice remaining to be iterated
105     string: &'a str,
106 }
107
108 impl<'a> Iterator<char> for Chars<'a> {
109     #[inline]
110     fn next(&mut self) -> Option<char> {
111         // Decode the next codepoint, then update
112         // the slice to be just the remaining part
113         if self.string.len() != 0 {
114             let CharRange {ch, next} = self.string.char_range_at(0);
115             unsafe {
116                 self.string = raw::slice_unchecked(self.string, next, self.string.len());
117             }
118             Some(ch)
119         } else {
120             None
121         }
122     }
123
124     #[inline]
125     fn size_hint(&self) -> (uint, Option<uint>) {
126         (self.string.len().saturating_add(3)/4, Some(self.string.len()))
127     }
128 }
129
130 impl<'a> DoubleEndedIterator<char> for Chars<'a> {
131     #[inline]
132     fn next_back(&mut self) -> Option<char> {
133         if self.string.len() != 0 {
134             let CharRange {ch, next} = self.string.char_range_at_reverse(self.string.len());
135             unsafe {
136                 self.string = raw::slice_unchecked(self.string, 0, next);
137             }
138             Some(ch)
139         } else {
140             None
141         }
142     }
143 }
144
145 /// External iterator for a string's characters and their byte offsets.
146 /// Use with the `std::iter` module.
147 #[deriving(Clone)]
148 pub struct CharOffsets<'a> {
149     /// The original string to be iterated
150     string: &'a str,
151     iter: Chars<'a>,
152 }
153
154 impl<'a> Iterator<(uint, char)> for CharOffsets<'a> {
155     #[inline]
156     fn next(&mut self) -> Option<(uint, char)> {
157         // Compute the byte offset by using the pointer offset between
158         // the original string slice and the iterator's remaining part
159         let offset = self.iter.string.as_ptr() as uint - self.string.as_ptr() as uint;
160         self.iter.next().map(|ch| (offset, ch))
161     }
162
163     #[inline]
164     fn size_hint(&self) -> (uint, Option<uint>) {
165         self.iter.size_hint()
166     }
167 }
168
169 impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> {
170     #[inline]
171     fn next_back(&mut self) -> Option<(uint, char)> {
172         self.iter.next_back().map(|ch| {
173             let offset = self.iter.string.len() +
174                     self.iter.string.as_ptr() as uint - self.string.as_ptr() as uint;
175             (offset, ch)
176         })
177     }
178 }
179
180 /// External iterator for a string's bytes.
181 /// Use with the `std::iter` module.
182 pub type Bytes<'a> =
183     Map<'a, &'a u8, u8, slice::Items<'a, u8>>;
184
185 /// An iterator over the substrings of a string, separated by `sep`.
186 #[deriving(Clone)]
187 pub struct CharSplits<'a, Sep> {
188     /// The slice remaining to be iterated
189     string: &'a str,
190     sep: Sep,
191     /// Whether an empty string at the end is allowed
192     allow_trailing_empty: bool,
193     only_ascii: bool,
194     finished: bool,
195 }
196
197 /// An iterator over the substrings of a string, separated by `sep`,
198 /// splitting at most `count` times.
199 #[deriving(Clone)]
200 pub struct CharSplitsN<'a, Sep> {
201     iter: CharSplits<'a, Sep>,
202     /// The number of splits remaining
203     count: uint,
204     invert: bool,
205 }
206
207 /// An iterator over the words of a string, separated by a sequence of whitespace
208 pub type Words<'a> =
209     Filter<'a, &'a str, CharSplits<'a, extern "Rust" fn(char) -> bool>>;
210
211 /// An iterator over the lines of a string, separated by either `\n` or (`\r\n`).
212 pub type AnyLines<'a> =
213     Map<'a, &'a str, &'a str, CharSplits<'a, char>>;
214
215 impl<'a, Sep> CharSplits<'a, Sep> {
216     #[inline]
217     fn get_end(&mut self) -> Option<&'a str> {
218         if !self.finished && (self.allow_trailing_empty || self.string.len() > 0) {
219             self.finished = true;
220             Some(self.string)
221         } else {
222             None
223         }
224     }
225 }
226
227 impl<'a, Sep: CharEq> Iterator<&'a str> for CharSplits<'a, Sep> {
228     #[inline]
229     fn next(&mut self) -> Option<&'a str> {
230         if self.finished { return None }
231
232         let mut next_split = None;
233         if self.only_ascii {
234             for (idx, byte) in self.string.bytes().enumerate() {
235                 if self.sep.matches(byte as char) && byte < 128u8 {
236                     next_split = Some((idx, idx + 1));
237                     break;
238                 }
239             }
240         } else {
241             for (idx, ch) in self.string.char_indices() {
242                 if self.sep.matches(ch) {
243                     next_split = Some((idx, self.string.char_range_at(idx).next));
244                     break;
245                 }
246             }
247         }
248         match next_split {
249             Some((a, b)) => unsafe {
250                 let elt = raw::slice_unchecked(self.string, 0, a);
251                 self.string = raw::slice_unchecked(self.string, b, self.string.len());
252                 Some(elt)
253             },
254             None => self.get_end(),
255         }
256     }
257 }
258
259 impl<'a, Sep: CharEq> DoubleEndedIterator<&'a str>
260 for CharSplits<'a, Sep> {
261     #[inline]
262     fn next_back(&mut self) -> Option<&'a str> {
263         if self.finished { return None }
264
265         if !self.allow_trailing_empty {
266             self.allow_trailing_empty = true;
267             match self.next_back() {
268                 Some(elt) if !elt.is_empty() => return Some(elt),
269                 _ => if self.finished { return None }
270             }
271         }
272         let len = self.string.len();
273         let mut next_split = None;
274
275         if self.only_ascii {
276             for (idx, byte) in self.string.bytes().enumerate().rev() {
277                 if self.sep.matches(byte as char) && byte < 128u8 {
278                     next_split = Some((idx, idx + 1));
279                     break;
280                 }
281             }
282         } else {
283             for (idx, ch) in self.string.char_indices().rev() {
284                 if self.sep.matches(ch) {
285                     next_split = Some((idx, self.string.char_range_at(idx).next));
286                     break;
287                 }
288             }
289         }
290         match next_split {
291             Some((a, b)) => unsafe {
292                 let elt = raw::slice_unchecked(self.string, b, len);
293                 self.string = raw::slice_unchecked(self.string, 0, a);
294                 Some(elt)
295             },
296             None => { self.finished = true; Some(self.string) }
297         }
298     }
299 }
300
301 impl<'a, Sep: CharEq> Iterator<&'a str> for CharSplitsN<'a, Sep> {
302     #[inline]
303     fn next(&mut self) -> Option<&'a str> {
304         if self.count != 0 {
305             self.count -= 1;
306             if self.invert { self.iter.next_back() } else { self.iter.next() }
307         } else {
308             self.iter.get_end()
309         }
310     }
311 }
312
313 /// The internal state of an iterator that searches for matches of a substring
314 /// within a larger string using naive search
315 #[deriving(Clone)]
316 struct NaiveSearcher {
317     position: uint
318 }
319
320 impl NaiveSearcher {
321     fn new() -> NaiveSearcher {
322         NaiveSearcher { position: 0 }
323     }
324
325     fn next(&mut self, haystack: &[u8], needle: &[u8]) -> Option<(uint, uint)> {
326         while self.position + needle.len() <= haystack.len() {
327             if haystack.slice(self.position, self.position + needle.len()) == needle {
328                 let matchPos = self.position;
329                 self.position += needle.len(); // add 1 for all matches
330                 return Some((matchPos, matchPos + needle.len()));
331             } else {
332                 self.position += 1;
333             }
334         }
335         None
336     }
337 }
338
339 /// The internal state of an iterator that searches for matches of a substring
340 /// within a larger string using two-way search
341 #[deriving(Clone)]
342 struct TwoWaySearcher {
343     // constants
344     critPos: uint,
345     period: uint,
346     byteset: u64,
347
348     // variables
349     position: uint,
350     memory: uint
351 }
352
353 impl TwoWaySearcher {
354     fn new(needle: &[u8]) -> TwoWaySearcher {
355         let (critPos1, period1) = TwoWaySearcher::maximal_suffix(needle, false);
356         let (critPos2, period2) = TwoWaySearcher::maximal_suffix(needle, true);
357
358         let critPos;
359         let period;
360         if critPos1 > critPos2 {
361             critPos = critPos1;
362             period = period1;
363         } else {
364             critPos = critPos2;
365             period = period2;
366         }
367
368         let byteset = needle.iter()
369                             .fold(0, |a, &b| (1 << ((b & 0x3f) as uint)) | a);
370
371         if needle.slice_to(critPos) == needle.slice_from(needle.len() - critPos) {
372             TwoWaySearcher {
373                 critPos: critPos,
374                 period: period,
375                 byteset: byteset,
376
377                 position: 0,
378                 memory: 0
379             }
380         } else {
381             TwoWaySearcher {
382                 critPos: critPos,
383                 period: cmp::max(critPos, needle.len() - critPos) + 1,
384                 byteset: byteset,
385
386                 position: 0,
387                 memory: uint::MAX // Dummy value to signify that the period is long
388             }
389         }
390     }
391
392     #[inline]
393     fn next(&mut self, haystack: &[u8], needle: &[u8], longPeriod: bool) -> Option<(uint, uint)> {
394         'search: loop {
395             // Check that we have room to search in
396             if self.position + needle.len() > haystack.len() {
397                 return None;
398             }
399
400             // Quickly skip by large portions unrelated to our substring
401             if (self.byteset >>
402                     ((haystack[self.position + needle.len() - 1] & 0x3f)
403                      as uint)) & 1 == 0 {
404                 self.position += needle.len();
405                 continue 'search;
406             }
407
408             // See if the right part of the needle matches
409             let start = if longPeriod { self.critPos } else { cmp::max(self.critPos, self.memory) };
410             for i in range(start, needle.len()) {
411                 if needle[i] != haystack[self.position + i] {
412                     self.position += i - self.critPos + 1;
413                     if !longPeriod {
414                         self.memory = 0;
415                     }
416                     continue 'search;
417                 }
418             }
419
420             // See if the left part of the needle matches
421             let start = if longPeriod { 0 } else { self.memory };
422             for i in range(start, self.critPos).rev() {
423                 if needle[i] != haystack[self.position + i] {
424                     self.position += self.period;
425                     if !longPeriod {
426                         self.memory = needle.len() - self.period;
427                     }
428                     continue 'search;
429                 }
430             }
431
432             // We have found a match!
433             let matchPos = self.position;
434             self.position += needle.len(); // add self.period for all matches
435             if !longPeriod {
436                 self.memory = 0; // set to needle.len() - self.period for all matches
437             }
438             return Some((matchPos, matchPos + needle.len()));
439         }
440     }
441
442     #[inline]
443     fn maximal_suffix(arr: &[u8], reversed: bool) -> (uint, uint) {
444         let mut left = -1; // Corresponds to i in the paper
445         let mut right = 0; // Corresponds to j in the paper
446         let mut offset = 1; // Corresponds to k in the paper
447         let mut period = 1; // Corresponds to p in the paper
448
449         while right + offset < arr.len() {
450             let a;
451             let b;
452             if reversed {
453                 a = arr[left + offset];
454                 b = arr[right + offset];
455             } else {
456                 a = arr[right + offset];
457                 b = arr[left + offset];
458             }
459             if a < b {
460                 // Suffix is smaller, period is entire prefix so far.
461                 right += offset;
462                 offset = 1;
463                 period = right - left;
464             } else if a == b {
465                 // Advance through repetition of the current period.
466                 if offset == period {
467                     right += offset;
468                     offset = 1;
469                 } else {
470                     offset += 1;
471                 }
472             } else {
473                 // Suffix is larger, start over from current location.
474                 left = right;
475                 right += 1;
476                 offset = 1;
477                 period = 1;
478             }
479         }
480         (left + 1, period)
481     }
482 }
483
484 /// The internal state of an iterator that searches for matches of a substring
485 /// within a larger string using a dynamically chosen search algorithm
486 #[deriving(Clone)]
487 enum Searcher {
488     Naive(NaiveSearcher),
489     TwoWay(TwoWaySearcher),
490     TwoWayLong(TwoWaySearcher)
491 }
492
493 impl Searcher {
494     fn new(haystack: &[u8], needle: &[u8]) -> Searcher {
495         // FIXME: Tune this.
496         if needle.len() > haystack.len() - 20 {
497             Naive(NaiveSearcher::new())
498         } else {
499             let searcher = TwoWaySearcher::new(needle);
500             if searcher.memory == uint::MAX { // If the period is long
501                 TwoWayLong(searcher)
502             } else {
503                 TwoWay(searcher)
504             }
505         }
506     }
507 }
508
509 /// An iterator over the start and end indices of the matches of a
510 /// substring within a larger string
511 #[deriving(Clone)]
512 pub struct MatchIndices<'a> {
513     // constants
514     haystack: &'a str,
515     needle: &'a str,
516     searcher: Searcher
517 }
518
519 /// An iterator over the substrings of a string separated by a given
520 /// search string
521 #[deriving(Clone)]
522 pub struct StrSplits<'a> {
523     it: MatchIndices<'a>,
524     last_end: uint,
525     finished: bool
526 }
527
528 impl<'a> Iterator<(uint, uint)> for MatchIndices<'a> {
529     #[inline]
530     fn next(&mut self) -> Option<(uint, uint)> {
531         match self.searcher {
532             Naive(ref mut searcher)
533                 => searcher.next(self.haystack.as_bytes(), self.needle.as_bytes()),
534             TwoWay(ref mut searcher)
535                 => searcher.next(self.haystack.as_bytes(), self.needle.as_bytes(), false),
536             TwoWayLong(ref mut searcher)
537                 => searcher.next(self.haystack.as_bytes(), self.needle.as_bytes(), true)
538         }
539     }
540 }
541
542 impl<'a> Iterator<&'a str> for StrSplits<'a> {
543     #[inline]
544     fn next(&mut self) -> Option<&'a str> {
545         if self.finished { return None; }
546
547         match self.it.next() {
548             Some((from, to)) => {
549                 let ret = Some(self.it.haystack.slice(self.last_end, from));
550                 self.last_end = to;
551                 ret
552             }
553             None => {
554                 self.finished = true;
555                 Some(self.it.haystack.slice(self.last_end, self.it.haystack.len()))
556             }
557         }
558     }
559 }
560
561 /// External iterator for a string's UTF16 codeunits.
562 /// Use with the `std::iter` module.
563 #[deriving(Clone)]
564 pub struct Utf16CodeUnits<'a> {
565     chars: Chars<'a>,
566     extra: u16
567 }
568
569 impl<'a> Iterator<u16> for Utf16CodeUnits<'a> {
570     #[inline]
571     fn next(&mut self) -> Option<u16> {
572         if self.extra != 0 {
573             let tmp = self.extra;
574             self.extra = 0;
575             return Some(tmp);
576         }
577
578         let mut buf = [0u16, ..2];
579         self.chars.next().map(|ch| {
580             let n = ch.encode_utf16(buf /* as mut slice! */);
581             if n == 2 { self.extra = buf[1]; }
582             buf[0]
583         })
584     }
585
586     #[inline]
587     fn size_hint(&self) -> (uint, Option<uint>) {
588         let (low, high) = self.chars.size_hint();
589         // every char gets either one u16 or two u16,
590         // so this iterator is between 1 or 2 times as
591         // long as the underlying iterator.
592         (low, high.and_then(|n| n.checked_mul(&2)))
593     }
594 }
595
596 /*
597 Section: Comparing strings
598 */
599
600 // share the implementation of the lang-item vs. non-lang-item
601 // eq_slice.
602 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
603 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
604 #[inline]
605 fn eq_slice_(a: &str, b: &str) -> bool {
606     #[allow(ctypes)]
607     extern { fn memcmp(s1: *const i8, s2: *const i8, n: uint) -> i32; }
608     a.len() == b.len() && unsafe {
609         memcmp(a.as_ptr() as *const i8,
610                b.as_ptr() as *const i8,
611                a.len()) == 0
612     }
613 }
614
615 /// Bytewise slice equality
616 /// NOTE: This function is (ab)used in rustc::middle::trans::_match
617 /// to compare &[u8] byte slices that are not necessarily valid UTF-8.
618 #[lang="str_eq"]
619 #[inline]
620 pub fn eq_slice(a: &str, b: &str) -> bool {
621     eq_slice_(a, b)
622 }
623
624 /*
625 Section: Misc
626 */
627
628 /// Walk through `iter` checking that it's a valid UTF-8 sequence,
629 /// returning `true` in that case, or, if it is invalid, `false` with
630 /// `iter` reset such that it is pointing at the first byte in the
631 /// invalid sequence.
632 #[inline(always)]
633 fn run_utf8_validation_iterator(iter: &mut slice::Items<u8>) -> bool {
634     loop {
635         // save the current thing we're pointing at.
636         let old = *iter;
637
638         // restore the iterator we had at the start of this codepoint.
639         macro_rules! err ( () => { {*iter = old; return false} });
640         macro_rules! next ( () => {
641                 match iter.next() {
642                     Some(a) => *a,
643                     // we needed data, but there was none: error!
644                     None => err!()
645                 }
646             });
647
648         let first = match iter.next() {
649             Some(&b) => b,
650             // we're at the end of the iterator and a codepoint
651             // boundary at the same time, so this string is valid.
652             None => return true
653         };
654
655         // ASCII characters are always valid, so only large
656         // bytes need more examination.
657         if first >= 128 {
658             let w = utf8_char_width(first);
659             let second = next!();
660             // 2-byte encoding is for codepoints  \u0080 to  \u07ff
661             //        first  C2 80        last DF BF
662             // 3-byte encoding is for codepoints  \u0800 to  \uffff
663             //        first  E0 A0 80     last EF BF BF
664             //   excluding surrogates codepoints  \ud800 to  \udfff
665             //               ED A0 80 to       ED BF BF
666             // 4-byte encoding is for codepoints \u10000 to \u10ffff
667             //        first  F0 90 80 80  last F4 8F BF BF
668             //
669             // Use the UTF-8 syntax from the RFC
670             //
671             // https://tools.ietf.org/html/rfc3629
672             // UTF8-1      = %x00-7F
673             // UTF8-2      = %xC2-DF UTF8-tail
674             // UTF8-3      = %xE0 %xA0-BF UTF8-tail / %xE1-EC 2( UTF8-tail ) /
675             //               %xED %x80-9F UTF8-tail / %xEE-EF 2( UTF8-tail )
676             // UTF8-4      = %xF0 %x90-BF 2( UTF8-tail ) / %xF1-F3 3( UTF8-tail ) /
677             //               %xF4 %x80-8F 2( UTF8-tail )
678             match w {
679                 2 => if second & 192 != TAG_CONT_U8 {err!()},
680                 3 => {
681                     match (first, second, next!() & 192) {
682                         (0xE0        , 0xA0 .. 0xBF, TAG_CONT_U8) |
683                         (0xE1 .. 0xEC, 0x80 .. 0xBF, TAG_CONT_U8) |
684                         (0xED        , 0x80 .. 0x9F, TAG_CONT_U8) |
685                         (0xEE .. 0xEF, 0x80 .. 0xBF, TAG_CONT_U8) => {}
686                         _ => err!()
687                     }
688                 }
689                 4 => {
690                     match (first, second, next!() & 192, next!() & 192) {
691                         (0xF0        , 0x90 .. 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
692                         (0xF1 .. 0xF3, 0x80 .. 0xBF, TAG_CONT_U8, TAG_CONT_U8) |
693                         (0xF4        , 0x80 .. 0x8F, TAG_CONT_U8, TAG_CONT_U8) => {}
694                         _ => err!()
695                     }
696                 }
697                 _ => err!()
698             }
699         }
700     }
701 }
702
703 /// Determines if a vector of bytes contains valid UTF-8.
704 pub fn is_utf8(v: &[u8]) -> bool {
705     run_utf8_validation_iterator(&mut v.iter())
706 }
707
708 /// Determines if a vector of `u16` contains valid UTF-16
709 pub fn is_utf16(v: &[u16]) -> bool {
710     let mut it = v.iter();
711     macro_rules! next ( ($ret:expr) => {
712             match it.next() { Some(u) => *u, None => return $ret }
713         }
714     )
715     loop {
716         let u = next!(true);
717
718         match char::from_u32(u as u32) {
719             Some(_) => {}
720             None => {
721                 let u2 = next!(false);
722                 if u < 0xD7FF || u > 0xDBFF ||
723                     u2 < 0xDC00 || u2 > 0xDFFF { return false; }
724             }
725         }
726     }
727 }
728
729 /// An iterator that decodes UTF-16 encoded codepoints from a vector
730 /// of `u16`s.
731 #[deriving(Clone)]
732 pub struct Utf16Items<'a> {
733     iter: slice::Items<'a, u16>
734 }
735 /// The possibilities for values decoded from a `u16` stream.
736 #[deriving(PartialEq, Eq, Clone, Show)]
737 pub enum Utf16Item {
738     /// A valid codepoint.
739     ScalarValue(char),
740     /// An invalid surrogate without its pair.
741     LoneSurrogate(u16)
742 }
743
744 impl Utf16Item {
745     /// Convert `self` to a `char`, taking `LoneSurrogate`s to the
746     /// replacement character (U+FFFD).
747     #[inline]
748     pub fn to_char_lossy(&self) -> char {
749         match *self {
750             ScalarValue(c) => c,
751             LoneSurrogate(_) => '\uFFFD'
752         }
753     }
754 }
755
756 impl<'a> Iterator<Utf16Item> for Utf16Items<'a> {
757     fn next(&mut self) -> Option<Utf16Item> {
758         let u = match self.iter.next() {
759             Some(u) => *u,
760             None => return None
761         };
762
763         if u < 0xD800 || 0xDFFF < u {
764             // not a surrogate
765             Some(ScalarValue(unsafe {mem::transmute(u as u32)}))
766         } else if u >= 0xDC00 {
767             // a trailing surrogate
768             Some(LoneSurrogate(u))
769         } else {
770             // preserve state for rewinding.
771             let old = self.iter;
772
773             let u2 = match self.iter.next() {
774                 Some(u2) => *u2,
775                 // eof
776                 None => return Some(LoneSurrogate(u))
777             };
778             if u2 < 0xDC00 || u2 > 0xDFFF {
779                 // not a trailing surrogate so we're not a valid
780                 // surrogate pair, so rewind to redecode u2 next time.
781                 self.iter = old;
782                 return Some(LoneSurrogate(u))
783             }
784
785             // all ok, so lets decode it.
786             let c = ((u - 0xD800) as u32 << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
787             Some(ScalarValue(unsafe {mem::transmute(c)}))
788         }
789     }
790
791     #[inline]
792     fn size_hint(&self) -> (uint, Option<uint>) {
793         let (low, high) = self.iter.size_hint();
794         // we could be entirely valid surrogates (2 elements per
795         // char), or entirely non-surrogates (1 element per char)
796         (low / 2, high)
797     }
798 }
799
800 /// Create an iterator over the UTF-16 encoded codepoints in `v`,
801 /// returning invalid surrogates as `LoneSurrogate`s.
802 ///
803 /// # Example
804 ///
805 /// ```rust
806 /// use std::str;
807 /// use std::str::{ScalarValue, LoneSurrogate};
808 ///
809 /// // 𝄞mus<invalid>ic<invalid>
810 /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
811 ///          0x0073, 0xDD1E, 0x0069, 0x0063,
812 ///          0xD834];
813 ///
814 /// assert_eq!(str::utf16_items(v).collect::<Vec<_>>(),
815 ///            vec![ScalarValue('𝄞'),
816 ///                 ScalarValue('m'), ScalarValue('u'), ScalarValue('s'),
817 ///                 LoneSurrogate(0xDD1E),
818 ///                 ScalarValue('i'), ScalarValue('c'),
819 ///                 LoneSurrogate(0xD834)]);
820 /// ```
821 pub fn utf16_items<'a>(v: &'a [u16]) -> Utf16Items<'a> {
822     Utf16Items { iter : v.iter() }
823 }
824
825 /// Return a slice of `v` ending at (and not including) the first NUL
826 /// (0).
827 ///
828 /// # Example
829 ///
830 /// ```rust
831 /// use std::str;
832 ///
833 /// // "abcd"
834 /// let mut v = ['a' as u16, 'b' as u16, 'c' as u16, 'd' as u16];
835 /// // no NULs so no change
836 /// assert_eq!(str::truncate_utf16_at_nul(v), v.as_slice());
837 ///
838 /// // "ab\0d"
839 /// v[2] = 0;
840 /// assert_eq!(str::truncate_utf16_at_nul(v),
841 ///            &['a' as u16, 'b' as u16]);
842 /// ```
843 pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
844     match v.iter().position(|c| *c == 0) {
845         // don't include the 0
846         Some(i) => v.slice_to(i),
847         None => v
848     }
849 }
850
851 // https://tools.ietf.org/html/rfc3629
852 static UTF8_CHAR_WIDTH: [u8, ..256] = [
853 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
854 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F
855 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
856 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F
857 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
858 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F
859 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
860 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F
861 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
862 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F
863 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
864 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF
865 0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
866 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF
867 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF
868 4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF
869 ];
870
871 /// Given a first byte, determine how many bytes are in this UTF-8 character
872 #[inline]
873 pub fn utf8_char_width(b: u8) -> uint {
874     return UTF8_CHAR_WIDTH[b as uint] as uint;
875 }
876
877 /// Struct that contains a `char` and the index of the first byte of
878 /// the next `char` in a string.  This can be used as a data structure
879 /// for iterating over the UTF-8 bytes of a string.
880 pub struct CharRange {
881     /// Current `char`
882     pub ch: char,
883     /// Index of the first byte of the next `char`
884     pub next: uint,
885 }
886
887 // Return the initial codepoint accumulator for the first byte.
888 // The first byte is special, only want bottom 5 bits for width 2, 4 bits
889 // for width 3, and 3 bits for width 4
890 macro_rules! utf8_first_byte(
891     ($byte:expr, $width:expr) => (($byte & (0x7F >> $width)) as u32)
892 )
893
894 // return the value of $ch updated with continuation byte $byte
895 macro_rules! utf8_acc_cont_byte(
896     ($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63u8) as u32)
897 )
898
899 static TAG_CONT_U8: u8 = 128u8;
900
901 /// Unsafe operations
902 pub mod raw {
903     use mem;
904     use collections::Collection;
905     use ptr::RawPtr;
906     use raw::Slice;
907     use slice::{ImmutableVector};
908     use str::{is_utf8, StrSlice};
909
910     /// Converts a slice of bytes to a string slice without checking
911     /// that the string contains valid UTF-8.
912     pub unsafe fn from_utf8<'a>(v: &'a [u8]) -> &'a str {
913         mem::transmute(v)
914     }
915
916     /// Form a slice from a C string. Unsafe because the caller must ensure the
917     /// C string has the static lifetime, or else the return value may be
918     /// invalidated later.
919     pub unsafe fn c_str_to_static_slice(s: *const i8) -> &'static str {
920         let s = s as *const u8;
921         let mut curr = s;
922         let mut len = 0u;
923         while *curr != 0u8 {
924             len += 1u;
925             curr = s.offset(len as int);
926         }
927         let v = Slice { data: s, len: len };
928         assert!(is_utf8(::mem::transmute(v)));
929         ::mem::transmute(v)
930     }
931
932     /// Takes a bytewise (not UTF-8) slice from a string.
933     ///
934     /// Returns the substring from [`begin`..`end`).
935     ///
936     /// # Failure
937     ///
938     /// If begin is greater than end.
939     /// If end is greater than the length of the string.
940     #[inline]
941     pub unsafe fn slice_bytes<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
942         assert!(begin <= end);
943         assert!(end <= s.len());
944         slice_unchecked(s, begin, end)
945     }
946
947     /// Takes a bytewise (not UTF-8) slice from a string.
948     ///
949     /// Returns the substring from [`begin`..`end`).
950     ///
951     /// Caller must check slice boundaries!
952     #[inline]
953     pub unsafe fn slice_unchecked<'a>(s: &'a str, begin: uint, end: uint) -> &'a str {
954         mem::transmute(Slice {
955                 data: s.as_ptr().offset(begin as int),
956                 len: end - begin,
957             })
958     }
959 }
960
961 /*
962 Section: Trait implementations
963 */
964
965 #[allow(missing_doc)]
966 pub mod traits {
967     use cmp::{Ord, Ordering, Less, Equal, Greater, PartialEq, PartialOrd, Equiv, Eq};
968     use collections::Collection;
969     use iter::Iterator;
970     use option::{Option, Some, None};
971     use str::{Str, StrSlice, eq_slice};
972
973     impl<'a> Ord for &'a str {
974         #[inline]
975         fn cmp(&self, other: & &'a str) -> Ordering {
976             for (s_b, o_b) in self.bytes().zip(other.bytes()) {
977                 match s_b.cmp(&o_b) {
978                     Greater => return Greater,
979                     Less => return Less,
980                     Equal => ()
981                 }
982             }
983
984             self.len().cmp(&other.len())
985         }
986     }
987
988     impl<'a> PartialEq for &'a str {
989         #[inline]
990         fn eq(&self, other: & &'a str) -> bool {
991             eq_slice((*self), (*other))
992         }
993         #[inline]
994         fn ne(&self, other: & &'a str) -> bool { !(*self).eq(other) }
995     }
996
997     impl<'a> Eq for &'a str {}
998
999     impl<'a> PartialOrd for &'a str {
1000         #[inline]
1001         fn partial_cmp(&self, other: &&'a str) -> Option<Ordering> {
1002             Some(self.cmp(other))
1003         }
1004     }
1005
1006     impl<'a, S: Str> Equiv<S> for &'a str {
1007         #[inline]
1008         fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) }
1009     }
1010 }
1011
1012 /// Any string that can be represented as a slice
1013 pub trait Str {
1014     /// Work with `self` as a slice.
1015     fn as_slice<'a>(&'a self) -> &'a str;
1016 }
1017
1018 impl<'a> Str for &'a str {
1019     #[inline]
1020     fn as_slice<'a>(&'a self) -> &'a str { *self }
1021 }
1022
1023 impl<'a> Collection for &'a str {
1024     #[inline]
1025     fn len(&self) -> uint {
1026         self.repr().len
1027     }
1028 }
1029
1030 /// Methods for string slices
1031 pub trait StrSlice<'a> {
1032     /// Returns true if one string contains another
1033     ///
1034     /// # Arguments
1035     ///
1036     /// - needle - The string to look for
1037     fn contains<'a>(&self, needle: &'a str) -> bool;
1038
1039     /// Returns true if a string contains a char.
1040     ///
1041     /// # Arguments
1042     ///
1043     /// - needle - The char to look for
1044     fn contains_char(&self, needle: char) -> bool;
1045
1046     /// An iterator over the characters of `self`. Note, this iterates
1047     /// over unicode code-points, not unicode graphemes.
1048     ///
1049     /// # Example
1050     ///
1051     /// ```rust
1052     /// let v: Vec<char> = "abc åäö".chars().collect();
1053     /// assert_eq!(v, vec!['a', 'b', 'c', ' ', 'å', 'ä', 'ö']);
1054     /// ```
1055     fn chars(&self) -> Chars<'a>;
1056
1057     /// An iterator over the bytes of `self`
1058     fn bytes(&self) -> Bytes<'a>;
1059
1060     /// An iterator over the characters of `self` and their byte offsets.
1061     fn char_indices(&self) -> CharOffsets<'a>;
1062
1063     /// An iterator over substrings of `self`, separated by characters
1064     /// matched by `sep`.
1065     ///
1066     /// # Example
1067     ///
1068     /// ```rust
1069     /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
1070     /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]);
1071     ///
1072     /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_digit()).collect();
1073     /// assert_eq!(v, vec!["abc", "def", "ghi"]);
1074     ///
1075     /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect();
1076     /// assert_eq!(v, vec!["lion", "", "tiger", "leopard"]);
1077     ///
1078     /// let v: Vec<&str> = "".split('X').collect();
1079     /// assert_eq!(v, vec![""]);
1080     /// ```
1081     fn split<Sep: CharEq>(&self, sep: Sep) -> CharSplits<'a, Sep>;
1082
1083     /// An iterator over substrings of `self`, separated by characters
1084     /// matched by `sep`, restricted to splitting at most `count`
1085     /// times.
1086     ///
1087     /// # Example
1088     ///
1089     /// ```rust
1090     /// let v: Vec<&str> = "Mary had a little lambda".splitn(' ', 2).collect();
1091     /// assert_eq!(v, vec!["Mary", "had", "a little lambda"]);
1092     ///
1093     /// let v: Vec<&str> = "abc1def2ghi".splitn(|c: char| c.is_digit(), 1).collect();
1094     /// assert_eq!(v, vec!["abc", "def2ghi"]);
1095     ///
1096     /// let v: Vec<&str> = "lionXXtigerXleopard".splitn('X', 2).collect();
1097     /// assert_eq!(v, vec!["lion", "", "tigerXleopard"]);
1098     ///
1099     /// let v: Vec<&str> = "abcXdef".splitn('X', 0).collect();
1100     /// assert_eq!(v, vec!["abcXdef"]);
1101     ///
1102     /// let v: Vec<&str> = "".splitn('X', 1).collect();
1103     /// assert_eq!(v, vec![""]);
1104     /// ```
1105     fn splitn<Sep: CharEq>(&self, sep: Sep, count: uint) -> CharSplitsN<'a, Sep>;
1106
1107     /// An iterator over substrings of `self`, separated by characters
1108     /// matched by `sep`.
1109     ///
1110     /// Equivalent to `split`, except that the trailing substring
1111     /// is skipped if empty (terminator semantics).
1112     ///
1113     /// # Example
1114     ///
1115     /// ```rust
1116     /// let v: Vec<&str> = "A.B.".split_terminator('.').collect();
1117     /// assert_eq!(v, vec!["A", "B"]);
1118     ///
1119     /// let v: Vec<&str> = "A..B..".split_terminator('.').collect();
1120     /// assert_eq!(v, vec!["A", "", "B", ""]);
1121     ///
1122     /// let v: Vec<&str> = "Mary had a little lamb".split(' ').rev().collect();
1123     /// assert_eq!(v, vec!["lamb", "little", "a", "had", "Mary"]);
1124     ///
1125     /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_digit()).rev().collect();
1126     /// assert_eq!(v, vec!["ghi", "def", "abc"]);
1127     ///
1128     /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect();
1129     /// assert_eq!(v, vec!["leopard", "tiger", "", "lion"]);
1130     /// ```
1131     fn split_terminator<Sep: CharEq>(&self, sep: Sep) -> CharSplits<'a, Sep>;
1132
1133     /// An iterator over substrings of `self`, separated by characters
1134     /// matched by `sep`, starting from the end of the string.
1135     /// Restricted to splitting at most `count` times.
1136     ///
1137     /// # Example
1138     ///
1139     /// ```rust
1140     /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(' ', 2).collect();
1141     /// assert_eq!(v, vec!["lamb", "little", "Mary had a"]);
1142     ///
1143     /// let v: Vec<&str> = "abc1def2ghi".rsplitn(|c: char| c.is_digit(), 1).collect();
1144     /// assert_eq!(v, vec!["ghi", "abc1def"]);
1145     ///
1146     /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn('X', 2).collect();
1147     /// assert_eq!(v, vec!["leopard", "tiger", "lionX"]);
1148     /// ```
1149     fn rsplitn<Sep: CharEq>(&self, sep: Sep, count: uint) -> CharSplitsN<'a, Sep>;
1150
1151     /// An iterator over the start and end indices of the disjoint
1152     /// matches of `sep` within `self`.
1153     ///
1154     /// That is, each returned value `(start, end)` satisfies
1155     /// `self.slice(start, end) == sep`. For matches of `sep` within
1156     /// `self` that overlap, only the indices corresponding to the
1157     /// first match are returned.
1158     ///
1159     /// # Example
1160     ///
1161     /// ```rust
1162     /// let v: Vec<(uint, uint)> = "abcXXXabcYYYabc".match_indices("abc").collect();
1163     /// assert_eq!(v, vec![(0,3), (6,9), (12,15)]);
1164     ///
1165     /// let v: Vec<(uint, uint)> = "1abcabc2".match_indices("abc").collect();
1166     /// assert_eq!(v, vec![(1,4), (4,7)]);
1167     ///
1168     /// let v: Vec<(uint, uint)> = "ababa".match_indices("aba").collect();
1169     /// assert_eq!(v, vec![(0, 3)]); // only the first `aba`
1170     /// ```
1171     fn match_indices(&self, sep: &'a str) -> MatchIndices<'a>;
1172
1173     /// An iterator over the substrings of `self` separated by `sep`.
1174     ///
1175     /// # Example
1176     ///
1177     /// ```rust
1178     /// let v: Vec<&str> = "abcXXXabcYYYabc".split_str("abc").collect();
1179     /// assert_eq!(v, vec!["", "XXX", "YYY", ""]);
1180     ///
1181     /// let v: Vec<&str> = "1abcabc2".split_str("abc").collect();
1182     /// assert_eq!(v, vec!["1", "", "2"]);
1183     /// ```
1184     fn split_str(&self, &'a str) -> StrSplits<'a>;
1185
1186     /// An iterator over the lines of a string (subsequences separated
1187     /// by `\n`). This does not include the empty string after a
1188     /// trailing `\n`.
1189     ///
1190     /// # Example
1191     ///
1192     /// ```rust
1193     /// let four_lines = "foo\nbar\n\nbaz\n";
1194     /// let v: Vec<&str> = four_lines.lines().collect();
1195     /// assert_eq!(v, vec!["foo", "bar", "", "baz"]);
1196     /// ```
1197     fn lines(&self) -> CharSplits<'a, char>;
1198
1199     /// An iterator over the lines of a string, separated by either
1200     /// `\n` or `\r\n`. As with `.lines()`, this does not include an
1201     /// empty trailing line.
1202     ///
1203     /// # Example
1204     ///
1205     /// ```rust
1206     /// let four_lines = "foo\r\nbar\n\r\nbaz\n";
1207     /// let v: Vec<&str> = four_lines.lines_any().collect();
1208     /// assert_eq!(v, vec!["foo", "bar", "", "baz"]);
1209     /// ```
1210     fn lines_any(&self) -> AnyLines<'a>;
1211
1212     /// An iterator over the words of a string (subsequences separated
1213     /// by any sequence of whitespace). Sequences of whitespace are
1214     /// collapsed, so empty "words" are not included.
1215     ///
1216     /// # Example
1217     ///
1218     /// ```rust
1219     /// let some_words = " Mary   had\ta little  \n\t lamb";
1220     /// let v: Vec<&str> = some_words.words().collect();
1221     /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]);
1222     /// ```
1223     fn words(&self) -> Words<'a>;
1224
1225     /// Returns true if the string contains only whitespace.
1226     ///
1227     /// Whitespace characters are determined by `char::is_whitespace`.
1228     ///
1229     /// # Example
1230     ///
1231     /// ```rust
1232     /// assert!(" \t\n".is_whitespace());
1233     /// assert!("".is_whitespace());
1234     ///
1235     /// assert!( !"abc".is_whitespace());
1236     /// ```
1237     fn is_whitespace(&self) -> bool;
1238
1239     /// Returns true if the string contains only alphanumeric code
1240     /// points.
1241     ///
1242     /// Alphanumeric characters are determined by `char::is_alphanumeric`.
1243     ///
1244     /// # Example
1245     ///
1246     /// ```rust
1247     /// assert!("Löwe老虎Léopard123".is_alphanumeric());
1248     /// assert!("".is_alphanumeric());
1249     ///
1250     /// assert!( !" &*~".is_alphanumeric());
1251     /// ```
1252     fn is_alphanumeric(&self) -> bool;
1253
1254     /// Returns the number of Unicode code points (`char`) that a
1255     /// string holds.
1256     ///
1257     /// This does not perform any normalization, and is `O(n)`, since
1258     /// UTF-8 is a variable width encoding of code points.
1259     ///
1260     /// *Warning*: The number of code points in a string does not directly
1261     /// correspond to the number of visible characters or width of the
1262     /// visible text due to composing characters, and double- and
1263     /// zero-width ones.
1264     ///
1265     /// See also `.len()` for the byte length.
1266     ///
1267     /// # Example
1268     ///
1269     /// ```rust
1270     /// // composed forms of `ö` and `é`
1271     /// let c = "Löwe 老虎 Léopard"; // German, Simplified Chinese, French
1272     /// // decomposed forms of `ö` and `é`
1273     /// let d = "Lo\u0308we 老虎 Le\u0301opard";
1274     ///
1275     /// assert_eq!(c.char_len(), 15);
1276     /// assert_eq!(d.char_len(), 17);
1277     ///
1278     /// assert_eq!(c.len(), 21);
1279     /// assert_eq!(d.len(), 23);
1280     ///
1281     /// // the two strings *look* the same
1282     /// println!("{}", c);
1283     /// println!("{}", d);
1284     /// ```
1285     fn char_len(&self) -> uint;
1286
1287     /// Returns a slice of the given string from the byte range
1288     /// [`begin`..`end`).
1289     ///
1290     /// This operation is `O(1)`.
1291     ///
1292     /// Fails when `begin` and `end` do not point to valid characters
1293     /// or point beyond the last character of the string.
1294     ///
1295     /// See also `slice_to` and `slice_from` for slicing prefixes and
1296     /// suffixes of strings, and `slice_chars` for slicing based on
1297     /// code point counts.
1298     ///
1299     /// # Example
1300     ///
1301     /// ```rust
1302     /// let s = "Löwe 老虎 Léopard";
1303     /// assert_eq!(s.slice(0, 1), "L");
1304     ///
1305     /// assert_eq!(s.slice(1, 9), "öwe 老");
1306     ///
1307     /// // these will fail:
1308     /// // byte 2 lies within `ö`:
1309     /// // s.slice(2, 3);
1310     ///
1311     /// // byte 8 lies within `老`
1312     /// // s.slice(1, 8);
1313     ///
1314     /// // byte 100 is outside the string
1315     /// // s.slice(3, 100);
1316     /// ```
1317     fn slice(&self, begin: uint, end: uint) -> &'a str;
1318
1319     /// Returns a slice of the string from `begin` to its end.
1320     ///
1321     /// Equivalent to `self.slice(begin, self.len())`.
1322     ///
1323     /// Fails when `begin` does not point to a valid character, or is
1324     /// out of bounds.
1325     ///
1326     /// See also `slice`, `slice_to` and `slice_chars`.
1327     fn slice_from(&self, begin: uint) -> &'a str;
1328
1329     /// Returns a slice of the string from the beginning to byte
1330     /// `end`.
1331     ///
1332     /// Equivalent to `self.slice(0, end)`.
1333     ///
1334     /// Fails when `end` does not point to a valid character, or is
1335     /// out of bounds.
1336     ///
1337     /// See also `slice`, `slice_from` and `slice_chars`.
1338     fn slice_to(&self, end: uint) -> &'a str;
1339
1340     /// Returns a slice of the string from the character range
1341     /// [`begin`..`end`).
1342     ///
1343     /// That is, start at the `begin`-th code point of the string and
1344     /// continue to the `end`-th code point. This does not detect or
1345     /// handle edge cases such as leaving a combining character as the
1346     /// first code point of the string.
1347     ///
1348     /// Due to the design of UTF-8, this operation is `O(end)`.
1349     /// See `slice`, `slice_to` and `slice_from` for `O(1)`
1350     /// variants that use byte indices rather than code point
1351     /// indices.
1352     ///
1353     /// Fails if `begin` > `end` or the either `begin` or `end` are
1354     /// beyond the last character of the string.
1355     ///
1356     /// # Example
1357     ///
1358     /// ```rust
1359     /// let s = "Löwe 老虎 Léopard";
1360     /// assert_eq!(s.slice_chars(0, 4), "Löwe");
1361     /// assert_eq!(s.slice_chars(5, 7), "老虎");
1362     /// ```
1363     fn slice_chars(&self, begin: uint, end: uint) -> &'a str;
1364
1365     /// Returns true if `needle` is a prefix of the string.
1366     fn starts_with(&self, needle: &str) -> bool;
1367
1368     /// Returns true if `needle` is a suffix of the string.
1369     fn ends_with(&self, needle: &str) -> bool;
1370
1371     /// Returns a string with leading and trailing whitespace removed.
1372     fn trim(&self) -> &'a str;
1373
1374     /// Returns a string with leading whitespace removed.
1375     fn trim_left(&self) -> &'a str;
1376
1377     /// Returns a string with trailing whitespace removed.
1378     fn trim_right(&self) -> &'a str;
1379
1380     /// Returns a string with characters that match `to_trim` removed.
1381     ///
1382     /// # Arguments
1383     ///
1384     /// * to_trim - a character matcher
1385     ///
1386     /// # Example
1387     ///
1388     /// ```rust
1389     /// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar")
1390     /// assert_eq!("12foo1bar12".trim_chars(&['1', '2']), "foo1bar")
1391     /// assert_eq!("123foo1bar123".trim_chars(|c: char| c.is_digit()), "foo1bar")
1392     /// ```
1393     fn trim_chars<C: CharEq>(&self, to_trim: C) -> &'a str;
1394
1395     /// Returns a string with leading `chars_to_trim` removed.
1396     ///
1397     /// # Arguments
1398     ///
1399     /// * to_trim - a character matcher
1400     ///
1401     /// # Example
1402     ///
1403     /// ```rust
1404     /// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11")
1405     /// assert_eq!("12foo1bar12".trim_left_chars(&['1', '2']), "foo1bar12")
1406     /// assert_eq!("123foo1bar123".trim_left_chars(|c: char| c.is_digit()), "foo1bar123")
1407     /// ```
1408     fn trim_left_chars<C: CharEq>(&self, to_trim: C) -> &'a str;
1409
1410     /// Returns a string with trailing `chars_to_trim` removed.
1411     ///
1412     /// # Arguments
1413     ///
1414     /// * to_trim - a character matcher
1415     ///
1416     /// # Example
1417     ///
1418     /// ```rust
1419     /// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar")
1420     /// assert_eq!("12foo1bar12".trim_right_chars(&['1', '2']), "12foo1bar")
1421     /// assert_eq!("123foo1bar123".trim_right_chars(|c: char| c.is_digit()), "123foo1bar")
1422     /// ```
1423     fn trim_right_chars<C: CharEq>(&self, to_trim: C) -> &'a str;
1424
1425     /// Check that `index`-th byte lies at the start and/or end of a
1426     /// UTF-8 code point sequence.
1427     ///
1428     /// The start and end of the string (when `index == self.len()`)
1429     /// are considered to be boundaries.
1430     ///
1431     /// Fails if `index` is greater than `self.len()`.
1432     ///
1433     /// # Example
1434     ///
1435     /// ```rust
1436     /// let s = "Löwe 老虎 Léopard";
1437     /// assert!(s.is_char_boundary(0));
1438     /// // start of `老`
1439     /// assert!(s.is_char_boundary(6));
1440     /// assert!(s.is_char_boundary(s.len()));
1441     ///
1442     /// // second byte of `ö`
1443     /// assert!(!s.is_char_boundary(2));
1444     ///
1445     /// // third byte of `老`
1446     /// assert!(!s.is_char_boundary(8));
1447     /// ```
1448     fn is_char_boundary(&self, index: uint) -> bool;
1449
1450     /// Pluck a character out of a string and return the index of the next
1451     /// character.
1452     ///
1453     /// This function can be used to iterate over the unicode characters of a
1454     /// string.
1455     ///
1456     /// # Example
1457     ///
1458     /// This example manually iterate through the characters of a
1459     /// string; this should normally by done by `.chars()` or
1460     /// `.char_indices`.
1461     ///
1462     /// ```rust
1463     /// use std::str::CharRange;
1464     ///
1465     /// let s = "中华Việt Nam";
1466     /// let mut i = 0u;
1467     /// while i < s.len() {
1468     ///     let CharRange {ch, next} = s.char_range_at(i);
1469     ///     println!("{}: {}", i, ch);
1470     ///     i = next;
1471     /// }
1472     /// ```
1473     ///
1474     /// ## Output
1475     ///
1476     /// ```ignore
1477     /// 0: 中
1478     /// 3: 华
1479     /// 6: V
1480     /// 7: i
1481     /// 8: ệ
1482     /// 11: t
1483     /// 12:
1484     /// 13: N
1485     /// 14: a
1486     /// 15: m
1487     /// ```
1488     ///
1489     /// # Arguments
1490     ///
1491     /// * s - The string
1492     /// * i - The byte offset of the char to extract
1493     ///
1494     /// # Return value
1495     ///
1496     /// A record {ch: char, next: uint} containing the char value and the byte
1497     /// index of the next unicode character.
1498     ///
1499     /// # Failure
1500     ///
1501     /// If `i` is greater than or equal to the length of the string.
1502     /// If `i` is not the index of the beginning of a valid UTF-8 character.
1503     fn char_range_at(&self, start: uint) -> CharRange;
1504
1505     /// Given a byte position and a str, return the previous char and its position.
1506     ///
1507     /// This function can be used to iterate over a unicode string in reverse.
1508     ///
1509     /// Returns 0 for next index if called on start index 0.
1510     ///
1511     /// # Failure
1512     ///
1513     /// If `i` is greater than the length of the string.
1514     /// If `i` is not an index following a valid UTF-8 character.
1515     fn char_range_at_reverse(&self, start: uint) -> CharRange;
1516
1517     /// Plucks the character starting at the `i`th byte of a string.
1518     ///
1519     /// # Failure
1520     ///
1521     /// If `i` is greater than or equal to the length of the string.
1522     /// If `i` is not the index of the beginning of a valid UTF-8 character.
1523     fn char_at(&self, i: uint) -> char;
1524
1525     /// Plucks the character ending at the `i`th byte of a string.
1526     ///
1527     /// # Failure
1528     ///
1529     /// If `i` is greater than the length of the string.
1530     /// If `i` is not an index following a valid UTF-8 character.
1531     fn char_at_reverse(&self, i: uint) -> char;
1532
1533     /// Work with the byte buffer of a string as a byte slice.
1534     fn as_bytes(&self) -> &'a [u8];
1535
1536     /// Returns the byte index of the first character of `self` that
1537     /// matches `search`.
1538     ///
1539     /// # Return value
1540     ///
1541     /// `Some` containing the byte index of the last matching character
1542     /// or `None` if there is no match
1543     ///
1544     /// # Example
1545     ///
1546     /// ```rust
1547     /// let s = "Löwe 老虎 Léopard";
1548     ///
1549     /// assert_eq!(s.find('L'), Some(0));
1550     /// assert_eq!(s.find('é'), Some(14));
1551     ///
1552     /// // the first space
1553     /// assert_eq!(s.find(|c: char| c.is_whitespace()), Some(5));
1554     ///
1555     /// // neither are found
1556     /// assert_eq!(s.find(&['1', '2']), None);
1557     /// ```
1558     fn find<C: CharEq>(&self, search: C) -> Option<uint>;
1559
1560     /// Returns the byte index of the last character of `self` that
1561     /// matches `search`.
1562     ///
1563     /// # Return value
1564     ///
1565     /// `Some` containing the byte index of the last matching character
1566     /// or `None` if there is no match.
1567     ///
1568     /// # Example
1569     ///
1570     /// ```rust
1571     /// let s = "Löwe 老虎 Léopard";
1572     ///
1573     /// assert_eq!(s.rfind('L'), Some(13));
1574     /// assert_eq!(s.rfind('é'), Some(14));
1575     ///
1576     /// // the second space
1577     /// assert_eq!(s.rfind(|c: char| c.is_whitespace()), Some(12));
1578     ///
1579     /// // searches for an occurrence of either `1` or `2`, but neither are found
1580     /// assert_eq!(s.rfind(&['1', '2']), None);
1581     /// ```
1582     fn rfind<C: CharEq>(&self, search: C) -> Option<uint>;
1583
1584     /// Returns the byte index of the first matching substring
1585     ///
1586     /// # Arguments
1587     ///
1588     /// * `needle` - The string to search for
1589     ///
1590     /// # Return value
1591     ///
1592     /// `Some` containing the byte index of the first matching substring
1593     /// or `None` if there is no match.
1594     ///
1595     /// # Example
1596     ///
1597     /// ```rust
1598     /// let s = "Löwe 老虎 Léopard";
1599     ///
1600     /// assert_eq!(s.find_str("老虎 L"), Some(6));
1601     /// assert_eq!(s.find_str("muffin man"), None);
1602     /// ```
1603     fn find_str(&self, &str) -> Option<uint>;
1604
1605     /// Retrieves the first character from a string slice and returns
1606     /// it. This does not allocate a new string; instead, it returns a
1607     /// slice that point one character beyond the character that was
1608     /// shifted. If the string does not contain any characters,
1609     /// a tuple of None and an empty string is returned instead.
1610     ///
1611     /// # Example
1612     ///
1613     /// ```rust
1614     /// let s = "Löwe 老虎 Léopard";
1615     /// let (c, s1) = s.slice_shift_char();
1616     /// assert_eq!(c, Some('L'));
1617     /// assert_eq!(s1, "öwe 老虎 Léopard");
1618     ///
1619     /// let (c, s2) = s1.slice_shift_char();
1620     /// assert_eq!(c, Some('ö'));
1621     /// assert_eq!(s2, "we 老虎 Léopard");
1622     /// ```
1623     fn slice_shift_char(&self) -> (Option<char>, &'a str);
1624
1625     /// Returns the byte offset of an inner slice relative to an enclosing outer slice.
1626     ///
1627     /// Fails if `inner` is not a direct slice contained within self.
1628     ///
1629     /// # Example
1630     ///
1631     /// ```rust
1632     /// let string = "a\nb\nc";
1633     /// let lines: Vec<&str> = string.lines().collect();
1634     /// let lines = lines.as_slice();
1635     ///
1636     /// assert!(string.subslice_offset(lines[0]) == 0); // &"a"
1637     /// assert!(string.subslice_offset(lines[1]) == 2); // &"b"
1638     /// assert!(string.subslice_offset(lines[2]) == 4); // &"c"
1639     /// ```
1640     fn subslice_offset(&self, inner: &str) -> uint;
1641
1642     /// Return an unsafe pointer to the strings buffer.
1643     ///
1644     /// The caller must ensure that the string outlives this pointer,
1645     /// and that it is not reallocated (e.g. by pushing to the
1646     /// string).
1647     fn as_ptr(&self) -> *const u8;
1648
1649     /// Return an iterator of `u16` over the string encoded as UTF-16.
1650     fn utf16_units(&self) -> Utf16CodeUnits<'a>;
1651 }
1652
1653 impl<'a> StrSlice<'a> for &'a str {
1654     #[inline]
1655     fn contains<'a>(&self, needle: &'a str) -> bool {
1656         self.find_str(needle).is_some()
1657     }
1658
1659     #[inline]
1660     fn contains_char(&self, needle: char) -> bool {
1661         self.find(needle).is_some()
1662     }
1663
1664     #[inline]
1665     fn chars(&self) -> Chars<'a> {
1666         Chars{string: *self}
1667     }
1668
1669     #[inline]
1670     fn bytes(&self) -> Bytes<'a> {
1671         self.as_bytes().iter().map(|&b| b)
1672     }
1673
1674     #[inline]
1675     fn char_indices(&self) -> CharOffsets<'a> {
1676         CharOffsets{string: *self, iter: self.chars()}
1677     }
1678
1679     #[inline]
1680     fn split<Sep: CharEq>(&self, sep: Sep) -> CharSplits<'a, Sep> {
1681         CharSplits {
1682             string: *self,
1683             only_ascii: sep.only_ascii(),
1684             sep: sep,
1685             allow_trailing_empty: true,
1686             finished: false,
1687         }
1688     }
1689
1690     #[inline]
1691     fn splitn<Sep: CharEq>(&self, sep: Sep, count: uint)
1692         -> CharSplitsN<'a, Sep> {
1693         CharSplitsN {
1694             iter: self.split(sep),
1695             count: count,
1696             invert: false,
1697         }
1698     }
1699
1700     #[inline]
1701     fn split_terminator<Sep: CharEq>(&self, sep: Sep)
1702         -> CharSplits<'a, Sep> {
1703         CharSplits {
1704             allow_trailing_empty: false,
1705             ..self.split(sep)
1706         }
1707     }
1708
1709     #[inline]
1710     fn rsplitn<Sep: CharEq>(&self, sep: Sep, count: uint)
1711         -> CharSplitsN<'a, Sep> {
1712         CharSplitsN {
1713             iter: self.split(sep),
1714             count: count,
1715             invert: true,
1716         }
1717     }
1718
1719     #[inline]
1720     fn match_indices(&self, sep: &'a str) -> MatchIndices<'a> {
1721         assert!(!sep.is_empty())
1722         MatchIndices {
1723             haystack: *self,
1724             needle: sep,
1725             searcher: Searcher::new(self.as_bytes(), sep.as_bytes())
1726         }
1727     }
1728
1729     #[inline]
1730     fn split_str(&self, sep: &'a str) -> StrSplits<'a> {
1731         StrSplits {
1732             it: self.match_indices(sep),
1733             last_end: 0,
1734             finished: false
1735         }
1736     }
1737
1738     #[inline]
1739     fn lines(&self) -> CharSplits<'a, char> {
1740         self.split_terminator('\n')
1741     }
1742
1743     fn lines_any(&self) -> AnyLines<'a> {
1744         self.lines().map(|line| {
1745             let l = line.len();
1746             if l > 0 && line[l - 1] == '\r' as u8 { line.slice(0, l - 1) }
1747             else { line }
1748         })
1749     }
1750
1751     #[inline]
1752     fn words(&self) -> Words<'a> {
1753         self.split(char::is_whitespace).filter(|s| !s.is_empty())
1754     }
1755
1756     #[inline]
1757     fn is_whitespace(&self) -> bool { self.chars().all(char::is_whitespace) }
1758
1759     #[inline]
1760     fn is_alphanumeric(&self) -> bool { self.chars().all(char::is_alphanumeric) }
1761
1762     #[inline]
1763     fn char_len(&self) -> uint { self.chars().count() }
1764
1765     #[inline]
1766     fn slice(&self, begin: uint, end: uint) -> &'a str {
1767         assert!(self.is_char_boundary(begin) && self.is_char_boundary(end));
1768         unsafe { raw::slice_bytes(*self, begin, end) }
1769     }
1770
1771     #[inline]
1772     fn slice_from(&self, begin: uint) -> &'a str {
1773         self.slice(begin, self.len())
1774     }
1775
1776     #[inline]
1777     fn slice_to(&self, end: uint) -> &'a str {
1778         assert!(self.is_char_boundary(end));
1779         unsafe { raw::slice_bytes(*self, 0, end) }
1780     }
1781
1782     fn slice_chars(&self, begin: uint, end: uint) -> &'a str {
1783         assert!(begin <= end);
1784         let mut count = 0;
1785         let mut begin_byte = None;
1786         let mut end_byte = None;
1787
1788         // This could be even more efficient by not decoding,
1789         // only finding the char boundaries
1790         for (idx, _) in self.char_indices() {
1791             if count == begin { begin_byte = Some(idx); }
1792             if count == end { end_byte = Some(idx); break; }
1793             count += 1;
1794         }
1795         if begin_byte.is_none() && count == begin { begin_byte = Some(self.len()) }
1796         if end_byte.is_none() && count == end { end_byte = Some(self.len()) }
1797
1798         match (begin_byte, end_byte) {
1799             (None, _) => fail!("slice_chars: `begin` is beyond end of string"),
1800             (_, None) => fail!("slice_chars: `end` is beyond end of string"),
1801             (Some(a), Some(b)) => unsafe { raw::slice_bytes(*self, a, b) }
1802         }
1803     }
1804
1805     #[inline]
1806     fn starts_with<'a>(&self, needle: &'a str) -> bool {
1807         let n = needle.len();
1808         self.len() >= n && needle.as_bytes() == self.as_bytes().slice_to(n)
1809     }
1810
1811     #[inline]
1812     fn ends_with(&self, needle: &str) -> bool {
1813         let (m, n) = (self.len(), needle.len());
1814         m >= n && needle.as_bytes() == self.as_bytes().slice_from(m - n)
1815     }
1816
1817     #[inline]
1818     fn trim(&self) -> &'a str {
1819         self.trim_left().trim_right()
1820     }
1821
1822     #[inline]
1823     fn trim_left(&self) -> &'a str {
1824         self.trim_left_chars(char::is_whitespace)
1825     }
1826
1827     #[inline]
1828     fn trim_right(&self) -> &'a str {
1829         self.trim_right_chars(char::is_whitespace)
1830     }
1831
1832     #[inline]
1833     fn trim_chars<C: CharEq>(&self, mut to_trim: C) -> &'a str {
1834         let cur = match self.find(|c: char| !to_trim.matches(c)) {
1835             None => "",
1836             Some(i) => unsafe { raw::slice_bytes(*self, i, self.len()) }
1837         };
1838         match cur.rfind(|c: char| !to_trim.matches(c)) {
1839             None => "",
1840             Some(i) => {
1841                 let right = cur.char_range_at(i).next;
1842                 unsafe { raw::slice_bytes(cur, 0, right) }
1843             }
1844         }
1845     }
1846
1847     #[inline]
1848     fn trim_left_chars<C: CharEq>(&self, mut to_trim: C) -> &'a str {
1849         match self.find(|c: char| !to_trim.matches(c)) {
1850             None => "",
1851             Some(first) => unsafe { raw::slice_bytes(*self, first, self.len()) }
1852         }
1853     }
1854
1855     #[inline]
1856     fn trim_right_chars<C: CharEq>(&self, mut to_trim: C) -> &'a str {
1857         match self.rfind(|c: char| !to_trim.matches(c)) {
1858             None => "",
1859             Some(last) => {
1860                 let next = self.char_range_at(last).next;
1861                 unsafe { raw::slice_bytes(*self, 0u, next) }
1862             }
1863         }
1864     }
1865
1866     #[inline]
1867     fn is_char_boundary(&self, index: uint) -> bool {
1868         if index == self.len() { return true; }
1869         if index > self.len() { return false; }
1870         let b = self[index];
1871         return b < 128u8 || b >= 192u8;
1872     }
1873
1874     #[inline]
1875     fn char_range_at(&self, i: uint) -> CharRange {
1876         if self[i] < 128u8 {
1877             return CharRange {ch: self[i] as char, next: i + 1 };
1878         }
1879
1880         // Multibyte case is a fn to allow char_range_at to inline cleanly
1881         fn multibyte_char_range_at(s: &str, i: uint) -> CharRange {
1882             let mut val = s[i] as u32;
1883             let w = UTF8_CHAR_WIDTH[val as uint] as uint;
1884             assert!((w != 0));
1885
1886             val = utf8_first_byte!(val, w);
1887             val = utf8_acc_cont_byte!(val, s[i + 1]);
1888             if w > 2 { val = utf8_acc_cont_byte!(val, s[i + 2]); }
1889             if w > 3 { val = utf8_acc_cont_byte!(val, s[i + 3]); }
1890
1891             return CharRange {ch: unsafe { mem::transmute(val) }, next: i + w};
1892         }
1893
1894         return multibyte_char_range_at(*self, i);
1895     }
1896
1897     #[inline]
1898     fn char_range_at_reverse(&self, start: uint) -> CharRange {
1899         let mut prev = start;
1900
1901         prev = prev.saturating_sub(1);
1902         if self[prev] < 128 { return CharRange{ch: self[prev] as char, next: prev} }
1903
1904         // Multibyte case is a fn to allow char_range_at_reverse to inline cleanly
1905         fn multibyte_char_range_at_reverse(s: &str, mut i: uint) -> CharRange {
1906             // while there is a previous byte == 10......
1907             while i > 0 && s[i] & 192u8 == TAG_CONT_U8 {
1908                 i -= 1u;
1909             }
1910
1911             let mut val = s[i] as u32;
1912             let w = UTF8_CHAR_WIDTH[val as uint] as uint;
1913             assert!((w != 0));
1914
1915             val = utf8_first_byte!(val, w);
1916             val = utf8_acc_cont_byte!(val, s[i + 1]);
1917             if w > 2 { val = utf8_acc_cont_byte!(val, s[i + 2]); }
1918             if w > 3 { val = utf8_acc_cont_byte!(val, s[i + 3]); }
1919
1920             return CharRange {ch: unsafe { mem::transmute(val) }, next: i};
1921         }
1922
1923         return multibyte_char_range_at_reverse(*self, prev);
1924     }
1925
1926     #[inline]
1927     fn char_at(&self, i: uint) -> char {
1928         self.char_range_at(i).ch
1929     }
1930
1931     #[inline]
1932     fn char_at_reverse(&self, i: uint) -> char {
1933         self.char_range_at_reverse(i).ch
1934     }
1935
1936     #[inline]
1937     fn as_bytes(&self) -> &'a [u8] {
1938         unsafe { mem::transmute(*self) }
1939     }
1940
1941     fn find<C: CharEq>(&self, mut search: C) -> Option<uint> {
1942         if search.only_ascii() {
1943             self.bytes().position(|b| search.matches(b as char))
1944         } else {
1945             for (index, c) in self.char_indices() {
1946                 if search.matches(c) { return Some(index); }
1947             }
1948             None
1949         }
1950     }
1951
1952     fn rfind<C: CharEq>(&self, mut search: C) -> Option<uint> {
1953         if search.only_ascii() {
1954             self.bytes().rposition(|b| search.matches(b as char))
1955         } else {
1956             for (index, c) in self.char_indices().rev() {
1957                 if search.matches(c) { return Some(index); }
1958             }
1959             None
1960         }
1961     }
1962
1963     fn find_str(&self, needle: &str) -> Option<uint> {
1964         if needle.is_empty() {
1965             Some(0)
1966         } else {
1967             self.match_indices(needle)
1968                 .next()
1969                 .map(|(start, _end)| start)
1970         }
1971     }
1972
1973     #[inline]
1974     fn slice_shift_char(&self) -> (Option<char>, &'a str) {
1975         if self.is_empty() {
1976             return (None, *self);
1977         } else {
1978             let CharRange {ch, next} = self.char_range_at(0u);
1979             let next_s = unsafe { raw::slice_bytes(*self, next, self.len()) };
1980             return (Some(ch), next_s);
1981         }
1982     }
1983
1984     fn subslice_offset(&self, inner: &str) -> uint {
1985         let a_start = self.as_ptr() as uint;
1986         let a_end = a_start + self.len();
1987         let b_start = inner.as_ptr() as uint;
1988         let b_end = b_start + inner.len();
1989
1990         assert!(a_start <= b_start);
1991         assert!(b_end <= a_end);
1992         b_start - a_start
1993     }
1994
1995     #[inline]
1996     fn as_ptr(&self) -> *const u8 {
1997         self.repr().data
1998     }
1999
2000     #[inline]
2001     fn utf16_units(&self) -> Utf16CodeUnits<'a> {
2002         Utf16CodeUnits{ chars: self.chars(), extra: 0}
2003     }
2004 }
2005
2006 impl<'a> Default for &'a str {
2007     fn default() -> &'a str { "" }
2008 }