Skip to content

Commit

Permalink
Improve bool conversions of faulty values
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 24, 2023
1 parent 074e068 commit d548c30
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
36 changes: 29 additions & 7 deletions src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::globals::CHAR_E;
use crate::globals::CHAR_f;
use crate::globals::CHAR_n;
use crate::globals::CHAR_t;
use crate::globals::FIELD_true;
use crate::globals::FIELD_null;
use crate::globals::FIELD_false;

use crate::JSON;
use crate::Object;
Expand Down Expand Up @@ -104,16 +107,35 @@ impl<N> ByteArrayConversions for [u8; N]
if (size == 0) { Option::none() }
else
{
let mut byte : u8 = self[0];
let mut result : Option<bool> = Option::none();

if (byte == QUOTATION_MARK)
if (self[0] == QUOTATION_MARK)
{
let json = self.parse(&mut 1, (size - 1), -1);
if (json.doc.len() != 0) { if (json.doc[0].value.len() != 0) { byte = json.doc[0].value[0]; } }
if (json.doc.len() != 0)
{
if (json.doc[0].value.len() != 0)
{
let byte : u8 = json.doc[0].value[0];
let OK = ((byte == CHAR_t) | (byte == CHAR_n) | (byte == CHAR_f));
if (OK) { result = Option::some(byte == CHAR_t); }
}
}
}
else
{
let mut literal_field = (self[0] as Field);
for i in 1..size
{
literal_field *= 256;
literal_field += (self[i] as Field);
}

let OK = ((literal_field == FIELD_true) | (literal_field == FIELD_null) | (literal_field == FIELD_false));
if (OK) { result = Option::some(literal_field == FIELD_true); }
}

let OK = ((byte == CHAR_t) | (byte == CHAR_n) | (byte == CHAR_f));
if (OK) { Option::some(byte == CHAR_t) } else { Option::none() }
result
}
}

Expand Down Expand Up @@ -216,8 +238,8 @@ impl<N> ByteArrayConversions for str<N>
{
unconstrained pub fn as_bool(self) -> Option<bool> { self.as_bytes().as_bool() }
unconstrained pub fn as_field(self) -> Option<Field> { self.as_bytes().as_field() }
unconstrained pub fn as_list(self) -> [[u8]] { self.as_bytes().as_list() }
unconstrained pub fn as_string(self) -> [u8] { self.as_bytes().as_slice() }
unconstrained pub fn as_list(self) -> [[u8]] { self.as_bytes().as_list() }
unconstrained pub fn as_object(self) -> JSON { self.as_bytes().as_object() }
}

Expand All @@ -236,7 +258,7 @@ impl JSON
let size : Field = bytes.len();
if (size == 0) { JSON::none() }
{
if (bytes[0] == QUOTATION_MARK) { bytes.as_object() }
if (bytes[0] == QUOTATION_MARK) { bytes.parse(&mut 1, (size - 1), -1) }
else
{
let mut result : JSON = JSON::none();
Expand Down
4 changes: 4 additions & 0 deletions src/globals.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ global CHAR_E : u8 = 0x45;
global CHAR_f : u8 = 0x66;
global CHAR_n : u8 = 0x6E;
global CHAR_t : u8 = 0x74;

global FIELD_true : Field = 0x74727565;
global FIELD_null : Field = 0x6E756C6C;
global FIELD_false : Field = 0x66616C7365;
10 changes: 3 additions & 7 deletions src/parse.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ use crate::globals::ZERO;
use crate::globals::POINT;
use crate::globals::CHAR_e;
use crate::globals::CHAR_E;
use crate::globals::FIELD_true;
use crate::globals::FIELD_null;
use crate::globals::FIELD_false;

use crate::JSON;
use crate::Object;
use crate::Property;

global FIELD_t : Field = 0x74;
global FIELD_tr : Field = 0x7472;
global FIELD_tru : Field = 0x747275;
global FIELD_true : Field = 0x74727565;
global FIELD_null : Field = 0x6E756C6C;
global FIELD_false : Field = 0x66616C7365;

trait PropertyLookup
{
fn get<N> (self, key : str<N>) -> [u8];
Expand Down

0 comments on commit d548c30

Please sign in to comment.