diff --git a/src/convert.nr b/src/convert.nr index e840094..64636d9 100644 --- a/src/convert.nr +++ b/src/convert.nr @@ -65,7 +65,7 @@ impl FieldConversion for [u8; N] { let start : Field = ((self[0] == MINUS) as Field); let mut result : [Field; 5] = [start, size, 0, 0, 0]; - let mut decimal_point : Field = 0; + let mut decimal_point : Field = 1; for i in start..size { if (result[0] != result[1]) @@ -77,11 +77,11 @@ impl FieldConversion for [u8; N] result[1] = i; // end of whole bytes result[2] = (i + 1); // start of fraction bytes - decimal_point = 1; + decimal_point = 3; } else if ((byte == CHAR_e) | (byte == CHAR_E)) { - result[1 + (decimal_point * 2)] = i; // 1 - end of whole bytes, 3 - end of fraction bytes + result[decimal_point] = i; // 1 - end of whole bytes, 3 - end of fraction bytes result[4] = (i + 1); // start of exponent bytes } else if ((byte == MINUS) | (byte == PLUS)) @@ -102,8 +102,7 @@ impl ByteArrayConversions for [u8; N] unconstrained pub fn as_bool(self) -> Option { - let size = self.len(); - + let size : Field = self.len(); if (size == 0) { Option::none() } else { @@ -142,28 +141,31 @@ impl ByteArrayConversions for [u8; N] unconstrained pub fn as_field(self) -> Option { - let size = self.len(); - - let mut offset = 0; let mut offsets : [Field; 5] = [0; 5]; + let mut offset : Field = 0; + let mut size : Field = self.len(); if (size != 0) { if (self[0] == QUOTATION_MARK) { - let json = self.parse(&mut 1, (size - 1), -1); + size -= 1; + let json = self.parse(&mut 1, size, -1); if (json.doc.len() != 0) { offsets = json.doc[0].value.get_offsets(); offset = 1; + offsets[0] += 1; + offsets[1] += 1; + offsets[4] += 1; } } else { offsets = self.get_offsets(); } } - let first : Field = offsets[0] + offset; - let second : Field = offsets[1] + offset; - let last : Field = offsets[4] + offset; + let first : Field = offsets[0]; + let second : Field = offsets[1]; + let last : Field = offsets[4]; let mut result : Option = Option::none(); @@ -175,7 +177,7 @@ impl ByteArrayConversions for [u8; N] if (last != offset) // apply exponent { - let power : Field = self.get_whole(last, size - offset); + let power : Field = self.get_whole(last, size); let mut exponent : Field = 1; for _ in 0..power { exponent *= 10; } @@ -210,8 +212,7 @@ impl ByteArrayConversions for [u8; N] unconstrained pub fn as_list(self) -> [[u8]] { - let size = self.len(); - + let size : Field = self.len(); if (size == 0) { [] } else if (self[0] == QUOTATION_MARK) { @@ -224,7 +225,7 @@ impl ByteArrayConversions for [u8; N] unconstrained pub fn as_object(self) -> JSON { - let mut size : Field = self.len(); + let size : Field = self.len(); if (size == 0) { JSON::none() } else { diff --git a/src/parse.nr b/src/parse.nr index ecc8e89..a8005a0 100644 --- a/src/parse.nr +++ b/src/parse.nr @@ -159,7 +159,7 @@ impl ByteArrayParser for [u8; N] let mut byte_prev : u8 = 0; - let child = !((index == 0) | (self[index] == crate::globals::QUOTATION_MARK)); + let child = (index != 0); // this works equally well to ((child_index + 1) != 0) let mut OK = (index != end); let mut value = true; let mut done = false; @@ -173,12 +173,11 @@ impl ByteArrayParser for [u8; N] let mut number = false; let mut literal = false; let mut array_value = false; - let mut minus = false; let mut digit_0 = false; let mut digit_1_9 = false; - let mut e_E = false; let mut fraction = false; let mut fraction_digit = false; + let mut e_E = false; let mut e_minus = false; let mut e_plus = false; let mut e_digit = false; @@ -199,6 +198,8 @@ impl ByteArrayParser for [u8; N] if (safe & value) { prop.value = prop.value.push_back(byte); done = !(string | array); } else if (safe & string) { prop.key = prop.key.push_back(byte); has_key = true; } else if (safe) { OK = (object & has_key); } + + byte_prev = byte; } else if ((byte == BEGIN_OBJECT) | (byte == BEGIN_ARRAY)) { @@ -206,8 +207,6 @@ impl ByteArrayParser for [u8; N] if (byte == BEGIN_ARRAY) { - OK &= (!array_value | (array_value & value_delimiter)); - prop.value = prop.value.push_back(byte); array_value = array; @@ -224,8 +223,6 @@ impl ByteArrayParser for [u8; N] OK = (child_json.doc.len() != 0); if (OK) { - if ((prop.value.len() as u64) > 2) { OK = value_delimiter; } - prop.value = prop.value.push_back(BEGIN_OBJECT); prop.value = prop.value.push_back(child_index as u8); prop.value = prop.value.push_back(END_OBJECT); @@ -246,10 +243,10 @@ impl ByteArrayParser for [u8; N] } else if ((byte == END_OBJECT) | (byte == END_ARRAY)) { - OK = (!value_delimiter | (value_delimiter & array_value)); - if (byte == END_ARRAY) { + OK = (!value_delimiter | array_value); + prop.value = prop.value.push_back(byte); arrays -= 1; @@ -258,10 +255,10 @@ impl ByteArrayParser for [u8; N] } else { - OK &= (object & ((prop.key.len() != 0) == (prop.value.len() != 0))); + OK = object; object = false; - done = OK; + done = true; if (child) { @@ -275,12 +272,10 @@ impl ByteArrayParser for [u8; N] OK = (array | !(number | literal)); string = true; - escaped = (byte_prev == BACKSLASH); + escaped = (self[index - (1 + ((index != 1) as Field))] == BACKSLASH); if (value) { - OK &= (!array_value | (array_value & value_delimiter)); - prop.value = prop.value.push_back(byte); array_value = true; } @@ -298,7 +293,6 @@ impl ByteArrayParser for [u8; N] { OK = ((object | array) & array_value); - array_value = false; key_delimiter = false; value_delimiter = true; done = (OK & !array); @@ -308,9 +302,9 @@ impl ByteArrayParser for [u8; N] prop.value = prop.value.push_back(byte); literal_field = 0; + array_value = false; number = false; literal = false; - minus = false; digit_0 = false; digit_1_9 = false; fraction = false; @@ -323,9 +317,12 @@ impl ByteArrayParser for [u8; N] } else if ((byte > SPACE) & (byte != BACKSLASH)) { + prop.value = prop.value.push_back(byte); + array_value = true; + if ((byte - ZERO) < 10) { - OK = (value & !(literal | digit_0) | (digit_0 & (fraction | e_E))); + OK = (value & (!(literal | digit_0) | fraction | e_E)); OK &= (!object | has_key); number = true; @@ -346,10 +343,9 @@ impl ByteArrayParser for [u8; N] } else if (byte == MINUS) { - OK = (!(minus | digit_0 | digit_1_9) | (e_E & !(e_minus | e_plus | e_digit))); + OK = (!number | (e_E & !(e_minus | e_plus | e_digit))); number = true; - minus = true; e_minus = e_E; } else if (byte == PLUS) @@ -366,21 +362,18 @@ impl ByteArrayParser for [u8; N] literal_field *= 256; literal_field += (byte as Field); } - - prop.value = prop.value.push_back(byte); - array_value = true; } else { - OK = (byte != 0x00) | ((byte == 0x00) & (!(object | array) & (index == end))); - done = ((number | literal) & !(array | (byte == BACKSLASH))); + OK = ((byte != 0x00) | (!array & (index == end))); + done = (!array & (number | literal)); } if (done) { OK = !(number | literal) - | ((digit_0 | digit_1_9) & (!fraction | fraction_digit) & (!e_E | e_digit)) - | ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false)); + | ((digit_0 | digit_1_9) & (!fraction | fraction_digit) & (!e_E | e_digit)) + | ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false)); if (OK & value) { @@ -393,7 +386,6 @@ impl ByteArrayParser for [u8; N] value = false; number = false; literal = false; - minus = false; digit_0 = false; digit_1_9 = false; fraction = false; @@ -403,18 +395,16 @@ impl ByteArrayParser for [u8; N] e_plus = false; e_digit = false; } - - byte_prev = byte; } } - if (OK & !(object | array | string)) + if (OK & !(object | array)) { if (json.is_none()) { - OK = (((digit_0 | digit_1_9) & (!fraction | fraction_digit) & (!e_E | e_digit))) - | (!number & ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false))) - | !value; + OK = ((digit_0 | digit_1_9) & (!fraction | fraction_digit) & (!e_E | e_digit)) + | (!number & ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false))) + | !value; if (OK) { json.doc = [prop]; } }