Skip to content
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

Allow underscores in integers #135

Merged
merged 3 commits into from Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "ron"
version = "0.4.0"
version = "0.4.1"
license = "MIT/Apache-2.0"
keywords = ["parser", "serde", "serialization"]
authors = [
Expand Down
2 changes: 1 addition & 1 deletion docs/grammar.md
Expand Up @@ -46,7 +46,7 @@ value = unsigned | signed | float | string | char | bool | option | list | map |

```ebnf
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
unsigned = ["0", ("x" | "b" | "o")], digit, { digit };
unsigned = ["0", ("x" | "b" | "o")], digit, { digit | '_' };
signed = ["+" | "-"], unsigned;
float = float_std | float_frac;
float_std = ["+" | "-"], digit, { digit }, ".", {digit}, [float_exp];
Expand Down
2 changes: 2 additions & 0 deletions src/de/error.rs
Expand Up @@ -52,6 +52,7 @@ pub enum ParseError {
NoSuchExtension(String),

UnclosedBlockComment,
UnderscoreAtBeginning,
UnexpectedByte(char),

Utf8Error(Utf8Error),
Expand Down Expand Up @@ -115,6 +116,7 @@ impl StdError for Error {

ParseError::Utf8Error(ref e) => e.description(),
ParseError::UnclosedBlockComment => "Unclosed block comment",
ParseError::UnderscoreAtBeginning => "Found underscore at the beginning",
ParseError::UnexpectedByte(_) => "Unexpected byte",
ParseError::TrailingCharacters => "Non-whitespace trailing characters",

Expand Down
8 changes: 8 additions & 0 deletions src/de/tests.rs
Expand Up @@ -292,3 +292,11 @@ fn test_byte_stream() {
from_str("BytesStruct( small:[1, 2], large:\"AQIDBA==\" )"),
);
}

#[test]
fn test_numbers() {
assert_eq!(
Ok(vec![1234, 12345, 123456, 1234567, 555_555]),
from_str("[1_234, 12_345, 1_2_3_4_5_6, 1_234_567, 5_55_55_5]"),
);
}
16 changes: 14 additions & 2 deletions src/parse.rs
Expand Up @@ -6,7 +6,7 @@ use std::str::{FromStr, from_utf8, from_utf8_unchecked};

use de::{Error, ParseError, Result};

const DIGITS: &[u8] = b"0123456789ABCDEFabcdef";
const DIGITS: &[u8] = b"0123456789ABCDEFabcdef_";
const FLOAT_CHARS: &[u8] = b"0123456789.+-eE";
const IDENT_FIRST: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
const IDENT_CHAR: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789";
Expand Down Expand Up @@ -486,8 +486,20 @@ impl<'a> Bytes<'a> {
return self.err(ParseError::ExpectedInteger);
}

let tmp;
let mut s = unsafe { from_utf8_unchecked(&self.bytes[0..num_bytes]) };

if s.as_bytes()[0] == b'_' {
return self.err(ParseError::UnderscoreAtBeginning);
}

if s.contains('_') {
tmp = s.replace('_', "");
s = &tmp;
}

let res = Num::from_str(
unsafe { from_utf8_unchecked(&self.bytes[0..num_bytes]) },
s,
base,
).map_err(|_| self.error(ParseError::ExpectedInteger));

Expand Down