Skip to content

Commit

Permalink
Refactor as_list and remove parse_value
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Suciu committed Nov 23, 2023
1 parent a9d76b8 commit 38d074c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 118 deletions.
172 changes: 73 additions & 99 deletions src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ trait ByteArrayConversions
fn as_field(self) -> Option<Field>;
fn as_string(self) -> [u8];
fn as_list(self) -> [[u8]];
fn as_object(self, parent : JSON) -> JSON;
fn as_json(self) -> JSON;
fn as_object(self) -> JSON;
}

impl<N> FieldConversion for [u8; N]
Expand Down Expand Up @@ -114,12 +113,19 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_bool(self) -> Option<bool>
{
let bytes : [u8] = self.parse_value();
let size = self.len();

if (bytes.len() == 0) { Option::none() }
if (size == 0) { Option::none() }
else
{
let byte = bytes[0];
let mut byte : u8 = self[0];

if (byte == 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]; } }
}

let OK = ((byte == CHAR_t) | (byte == CHAR_n) | (byte == CHAR_f));
if (OK) { Option::some(byte == CHAR_t) } else { Option::none() }
}
Expand All @@ -128,30 +134,45 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_field(self) -> Option<Field>
{
let bytes : [u8] = self.parse_value();
let size = self.len();

let mut offsets : [Field; 5] = bytes.get_offsets();
let mut offset = 0;
let mut offsets : [Field; 5] = [0; 5];

let first : Field = offsets[0];
let second : Field = offsets[1];
let last : Field = offsets[4];
if (size != 0)
{
if (self[0] == QUOTATION_MARK)
{
let json = self.parse(&mut 1, (size - 1), -1);
if (json.doc.len() != 0)
{
offsets = json.doc[0].value.get_offsets();
offset = 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 mut result : Option<Field> = Option::none();

// expect whole to have at least one digit and decimal point to be missing
if ((first != second) & (offsets[2] == offsets[3]))
{
let mut whole : Field = bytes.get_whole(first, second);
if (first == 1) { whole = -whole; }
let mut whole : Field = self.get_whole(first, second);
if (first == (1 + offset)) { whole = -whole; }

if (last != 0) // apply exponent
if (last != offset) // apply exponent
{
let power : Field = bytes.get_whole(last, bytes.len());
let power : Field = self.get_whole(last, size - offset);

let mut exponent : Field = 1;
for _ in 0..power { exponent *= 10; }

if (bytes[last - 1] == MINUS) { whole /= exponent; } else { whole *= exponent; }
if (self[last - 1] == MINUS) { whole /= exponent; } else { whole *= exponent; }
}

result = Option::some(whole);
Expand Down Expand Up @@ -181,70 +202,19 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_list(self) -> [[u8]]
{
let bytes : [u8] = self.parse_value();
let size = self.len();

let mut size : Field = bytes.len();
if (size == 0) | (size == 1) { [] }
else
if (size == 0) { [] }
else if (self[0] == QUOTATION_MARK)
{
size -= 1;
if ((bytes[0] == BEGIN_ARRAY) & (bytes[size] == END_ARRAY))
{
let mut result : [[u8]] = [];
let mut value : [u8] = [];
let mut objects : Field = 0;
let mut arrays : Field = 0;
for i in 1..size
{
let byte = bytes[i];

arrays += (((byte == BEGIN_ARRAY) as Field) - ((byte == END_ARRAY) as Field));
objects += (((byte == BEGIN_OBJECT) as Field) - ((byte == END_OBJECT) as Field));

if ((byte == VALUE_DELIMITER) & (arrays == 0) & (objects == 0))
{
result = result.push_back(value);
value = [];
}
else { value = value.push_back(byte); }
}

result.push_back(value)
}
else { [] }
let json = self.parse(&mut 1, (size - 1), -1);
if (json.doc.len() != 0) { json.doc[0].value.parse_list() } else { [] }
}
else { self.parse_list() }
}

unconstrained
pub fn as_object(self, parent : JSON) -> JSON
{
let mut result : JSON = JSON::none();

let size : Field = self.len();
if (size != 0)
{
if (self[0] == QUOTATION_MARK)
{
result = self.parse(&mut 1, (self.len() - 1), -1);
}
else
{
let OK = ((self[0] == BEGIN_OBJECT) & (self[size - 1] == END_OBJECT));

if (OK & (size == 3))
{
let index : u8 = self[1];
if (index < (parent.children.len() as u8)) { result = parent.child(index); }
}
else if (OK & (size != 1)) { result.doc = [crate::Property::none()]; }
}
}

result
}

unconstrained
pub fn as_json(self) -> JSON
pub fn as_object(self) -> JSON
{
let mut size : Field = self.len();
if (size == 0) { JSON::none() }
Expand All @@ -258,39 +228,43 @@ impl<N> ByteArrayConversions for [u8; N]

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_object(self, parent : JSON) -> JSON { self.as_bytes().as_object(parent) }

unconstrained
pub fn as_json(self) -> JSON { self.as_bytes().as_json() }
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_object(self) -> JSON { self.as_bytes().as_object() }
}

impl JSON
{
unconstrained
pub fn get_bool<N> (self, key : str<N>) -> Option<bool> { self.doc.get(key).as_bool() }
unconstrained pub fn get_bool<N> (self, key : str<N>) -> Option<bool> { self.doc.get(key).as_bool() }
unconstrained pub fn get_field<N> (self, key : str<N>) -> Option<Field> { self.doc.get(key).as_field() }
unconstrained pub fn get_string<N>(self, key : str<N>) -> [u8] { self.doc.get(key).as_string() }
unconstrained pub fn get_array<N> (self, key : str<N>) -> [[u8]] { self.doc.get(key).as_list() }

unconstrained
pub fn get_field<N> (self, key : str<N>) -> Option<Field> { self.doc.get(key).as_field() }
pub fn get_object<N>(self, key : str<N>) -> JSON
{
let bytes = self.doc.get(key);

unconstrained
pub fn get_string<N>(self, key : str<N>) -> [u8] { self.doc.get(key).as_string() }
let size : Field = bytes.len();
if (size == 0) { JSON::none() }
{
if (bytes[0] == QUOTATION_MARK) { bytes.as_object() }
else
{
let mut result : JSON = JSON::none();

unconstrained
pub fn get_array<N> (self, key : str<N>) -> [[u8]] { self.doc.get(key).as_list() }
let OK = ((bytes[0] == BEGIN_OBJECT) & (bytes[size - 1] == END_OBJECT));
if (OK & (size == 3))
{
let index : u8 = bytes[1];
if (index < (self.children.len() as u8)) { result = self.child(index); }
}
else if (OK & (size != 1)) { result.doc = [crate::Property::none()]; }

unconstrained
pub fn get_object<N>(self, key : str<N>) -> JSON { self.doc.get(key).as_object(self) }
result
}
}
}
}
11 changes: 2 additions & 9 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod utils;

struct Property
{
key : [u8],
value : [u8],
key : [u8]
, value : [u8]
}

type Object = [Property];
Expand All @@ -20,7 +20,6 @@ struct JSON
trait Extensions
{
fn none() -> Self;
fn len(self) -> Field;
fn is_none(self) -> bool;
fn is_empty(self) -> bool;
fn print(self);
Expand Down Expand Up @@ -74,9 +73,6 @@ impl Extensions for Object
unconstrained
pub fn is_none(self) -> bool { self.len() == 0 }

unconstrained
pub fn len(self) -> Field { self.len() }

unconstrained
pub fn is_empty(self) -> bool { if (self.len() == 1) { self[0].is_none() } else { false } }

Expand All @@ -92,9 +88,6 @@ impl Extensions for JSON
unconstrained
pub fn is_none(self) -> bool { self.doc.len() == 0 }

unconstrained
pub fn len(self) -> Field { self.doc.len() }

unconstrained
pub fn is_empty(self) -> bool { self.doc.is_empty() }

Expand Down
42 changes: 32 additions & 10 deletions src/parse.nr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait PropertyLookup

trait ByteArrayParser
{
fn parse_value(self) -> [u8];
fn parse_list(self) -> [[u8]];
fn parse(self, begin : &mut Field, end : Field, child_index : Field) -> JSON;
}

Expand Down Expand Up @@ -76,6 +76,9 @@ impl JSON
// if (inserted) { copy } else { copy.push_back(prop) }
}

unconstrained
pub fn len(self) -> Field { self.doc.len() }

unconstrained
pub fn get<N> (self, key : str<N>) -> [u8] { self.doc.get(key) }
}
Expand Down Expand Up @@ -113,20 +116,39 @@ impl PropertyLookup for Object
impl<N> ByteArrayParser for [u8; N]
{
unconstrained
pub fn parse_value(self) -> [u8]
fn parse_list(self) -> [[u8]]
{
let mut bytes : [u8] = self.as_slice();

if (self.len() != 0)
let mut size : Field = self.len();
if (size == 0) | (size == 1) { [] }
else
{
if (self[0] == QUOTATION_MARK)
size -= 1;
if ((self[0] == BEGIN_ARRAY) & (self[size] == END_ARRAY))
{
let json = self.parse(&mut 1, (self.len() - 1), -1);
if (json.doc.len() == 1) { bytes = json.doc[0].value; }
let mut result : [[u8]] = [];

let mut value : [u8] = [];
let mut objects : Field = 0;
let mut arrays : Field = 0;
for i in 1..size
{
let byte = self[i];

arrays += (((byte == BEGIN_ARRAY) as Field) - ((byte == END_ARRAY) as Field));
objects += (((byte == BEGIN_OBJECT) as Field) - ((byte == END_OBJECT) as Field));

if ((byte == VALUE_DELIMITER) & (arrays == 0) & (objects == 0))
{
result = result.push_back(value);
value = [];
}
else { value = value.push_back(byte); }
}

result.push_back(value)
}
else { [] }
}

bytes
}

unconstrained
Expand Down

0 comments on commit 38d074c

Please sign in to comment.