1// Searching and parsing byte text.2//3// These extend `&[u8]`, the type every text-taking function and string4// literal uses, so they apply to a String through its view():5// `line.view().starts_with(&"#")`. Searches scan for the needle's first6// byte and compare the rest only where it matches.78import std::option;910// True when the text begins with the prefix.11exp fn starts_with(self text: &[u8], prefix: &[u8]) -> bool {12 return prefix.length() <= text.length() && text[..prefix.length()] == prefix;13}1415// True when the text ends with the suffix.16exp fn ends_with(self text: &[u8], suffix: &[u8]) -> bool {17 return suffix.length() <= text.length() && text[text.length() - suffix.length()..] == suffix;18}1920// Where the needle first occurs at or after `from`, or `None`. An empty21// needle occurs at `from` itself.22exp fn find(self text: &[u8], needle: &[u8], from: u64) -> Option<u64> {23 const n = needle.length();24 if (from > text.length() || n > text.length() - from) {25 return ::None;26 }27 if (n == 0) {28 return ::Some(from);29 }30 const first = needle[0];31 for ([from..text.length() - n + 1]) |at| {32 if (text[at] == first && text[at + 1..at + n] == needle[1..]) {33 return ::Some(at);34 }35 }36 return ::None;37}3839// Where the byte first occurs at or after `from`, or `None`.40exp fn find_byte(self text: &[u8], byte: u8, from: u64) -> Option<u64> {41 if (from < text.length()) {42 for ([from..text.length()]) |at| {43 if (text[at] == byte) {44 return ::Some(at);45 }46 }47 }48 return ::None;49}5051// True when the needle occurs in the text.52exp fn contains(self text: &[u8], needle: &[u8]) -> bool {53 return text.find(&needle, 0) is ::Some;54}5556// The text without the spaces, tabs, carriage returns and newlines at57// either end.58exp fn trim(self text: &[u8]) -> &[u8] {59 var start: u64 = 0;60 var end = text.length();61 while (start < end && blank(text[start])) {62 start += 1;63 }64 while (end > start && blank(text[end - 1])) {65 end -= 1;66 }67 return &text[start..end];68}6970fn blank(byte: u8) -> bool {71 return byte == ' ' || byte == '\t' || byte == '\r' || byte == '\n';72}7374// The decimal integer the whole text spells, with an optional leading75// `'-'` or `'+'`, or `None` when it spells none or one out of range.76exp fn parse_integer(self text: &[u8]) -> Option<i64> {77 var at: u64 = 0;78 var negative = false;79 if (text.length() != 0 && (text[0] == '-' || text[0] == '+')) {80 negative = text[0] == '-';81 at = 1;82 }83 if (at == text.length()) {84 return ::None;85 }86 // the magnitude, up to 2^63 for the most negative value87 const limit: u64 = if (negative) 9223372036854775808 else 9223372036854775807;88 var magnitude: u64 = 0;89 for (text[at..]) |byte| {90 if (byte < '0' || byte > '9') {91 return ::None;92 }93 const digit = (byte - '0') to u64;94 if (magnitude > (limit - digit) / 10) {95 return ::None;96 }97 magnitude = magnitude * 10 + digit;98 }99 if (!negative) {100 return ::Some(magnitude as i64);101 }102 if (magnitude == 9223372036854775808) {103 return ::Some(magnitude as i64);104 }105 return ::Some(-(magnitude as i64));106}