unescape-0.1.0/.gitignore000064400017500001750000000000221266640534000135230ustar0000000000000000target Cargo.lock unescape-0.1.0/.travis.yml000064400017500001750000000001561266643660100136600ustar0000000000000000language: rust script: - cargo build --verbose - cargo test --verbose notifications: email: false unescape-0.1.0/Cargo.toml000064400017500001750000000014701266644020600134730ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "unescape" version = "0.1.0" authors = ["Saghm Rossi "] description = "Unescapes strings with escape sequences written out as literal characters." readme = "README.md" keywords = ["unescape", "unicode", "escape", "string", "strings"] license = "MIT" repository = "https://github.com/saghm/unescape-rs" [dependencies] unescape-0.1.0/Cargo.toml.orig000064400017500001750000000005451266644020600144340ustar0000000000000000[package] name = "unescape" version = "0.1.0" authors = ["Saghm Rossi "] description = "Unescapes strings with escape sequences written out as literal characters." repository = "https://github.com/saghm/unescape-rs" readme = "README.md" keywords = ["unescape", "unicode", "escape", "string", "strings"] license = "MIT" [dependencies] unescape-0.1.0/LICENSE000064400017500001750000000020661266644024600125560ustar0000000000000000The MIT License (MIT) Copyright (c) 2016 Saghm Rossi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. unescape-0.1.0/README.md000064400017500001750000000007411266643774200130350ustar0000000000000000js.rs — Parser [![Build Status](https://travis-ci.org/saghm/unescape-rs.svg)](https://travis-ci.org/saghm/unescape-rs) ==================================================================================================================================== "Unescapes" (JavaScript) strings with escape sequences written out as literal characters. ``` extern crate unescape; use unescape::unescape; fn main() { println!("{}", unescape(r"\u0038\u0039")); // prints out "89" } ``` unescape-0.1.0/src/lib.rs000064400017500001750000000052501266643652700134610ustar0000000000000000use std::collections::VecDeque; use std::char; macro_rules! try_option { ($o:expr) => { match $o { Some(s) => s, None => return None, } } } // Takes in a string with backslash escapes written out with literal backslash characters and // converts it to a string with the proper escaped characters. pub fn unescape(s: &str) -> Option { let mut queue : VecDeque<_> = String::from(s).chars().collect(); let mut s = String::new(); while let Some(c) = queue.pop_front() { if c != '\\' { s.push(c); continue; } match queue.pop_front() { Some('b') => s.push('\u{0008}'), Some('f') => s.push('\u{000C}'), Some('n') => s.push('\n'), Some('r') => s.push('\r'), Some('t') => s.push('\t'), Some('\'') => s.push('\''), Some('\"') => s.push('\"'), Some('\\') => s.push('\\'), Some('u') => s.push(try_option!(unescape_unicode(&mut queue))), Some('x') => s.push(try_option!(unescape_byte(&mut queue))), Some(c) if c.is_digit(8) => s.push(try_option!(unescape_octal(c, &mut queue))), _ => return None }; } Some(s) } fn unescape_unicode(queue: &mut VecDeque) -> Option { let mut s = String::new(); for _ in 0..4 { s.push(try_option!(queue.pop_front())); } let u = try_option!(u32::from_str_radix(&s, 16).ok()); char::from_u32(u) } fn unescape_byte(queue: &mut VecDeque) -> Option { let mut s = String::new(); for _ in 0..2 { s.push(try_option!(queue.pop_front())); } let u = try_option!(u32::from_str_radix(&s, 16).ok()); char::from_u32(u) } fn unescape_octal(c: char, queue: &mut VecDeque) -> Option { match unescape_octal_leading(c, queue) { Some(ch) => { let _ = queue.pop_front(); let _ = queue.pop_front(); Some(ch) } None => unescape_octal_no_leading(c, queue) } } fn unescape_octal_leading(c: char, queue: &VecDeque) -> Option { if c != '0' && c != '1' && c != '2' && c != '3' { return None; } let mut s = String::new(); s.push(c); s.push(*try_option!(queue.get(0))); s.push(*try_option!(queue.get(1))); let u = try_option!(u32::from_str_radix(&s, 8).ok()); char::from_u32(u) } fn unescape_octal_no_leading(c: char, queue: &mut VecDeque) -> Option { let mut s = String::new(); s.push(c); s.push(try_option!(queue.pop_front())); let u = try_option!(u32::from_str_radix(&s, 8).ok()); char::from_u32(u) } unescape-0.1.0/tests/lib.rs000064400017500001750000000023711266643763200140330ustar0000000000000000extern crate unescape; use unescape::unescape; macro_rules! assert_some_string { ($s1:expr, $s2:expr) => { assert_eq!(Some(String::from($s1)), unescape($s2)); } } #[test] fn no_escapes() { assert_some_string!("", ""); assert_some_string!("hello", "hello"); assert_some_string!("these are some pretty crazy strings here", "these are some pretty crazy strings here"); } #[test] fn control_chars() { assert_some_string!("First line\nSecond line", r"First line\nSecond line"); assert_some_string!("First line\r\nSecond line", r"First line\r\nSecond line"); assert_some_string!("Unindented\tIndented", r"Unindented\tIndented"); assert_some_string!("'This is singly quoted!'", r"\'This is singly quoted!\'"); assert_some_string!("\"This is doubly quoted!\"", r#"\"This is doubly quoted!\""#); assert_some_string!("This is one backslash: \\", r"This is one backslash: \\"); } #[test] fn unicode_chars() { assert_some_string!("\n", r"\u000A"); assert_some_string!("\u{1234}", r"\u1234"); } #[test] fn byte_chars() { assert_some_string!("\n", r"\x0A"); assert_some_string!("\x23", r"\x23"); } #[test] fn octal_chars() { assert_some_string!("\n", r"\12"); assert_some_string!("\u{00C4}", r"\304"); }