Skip to content

Commit

Permalink
Reduce conditionals and control variables
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 27, 2023
1 parent 15abcf1 commit 7419e1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
33 changes: 17 additions & 16 deletions src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<N> 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])
Expand All @@ -77,11 +77,11 @@ impl<N> 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))
Expand All @@ -102,8 +102,7 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_bool(self) -> Option<bool>
{
let size = self.len();

let size : Field = self.len();
if (size == 0) { Option::none() }
else
{
Expand Down Expand Up @@ -142,28 +141,31 @@ impl<N> ByteArrayConversions for [u8; N]
unconstrained
pub fn as_field(self) -> Option<Field>
{
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<Field> = Option::none();

Expand All @@ -175,7 +177,7 @@ impl<N> 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; }
Expand Down Expand Up @@ -210,8 +212,7 @@ impl<N> 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)
{
Expand All @@ -224,7 +225,7 @@ impl<N> 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
{
Expand Down
56 changes: 23 additions & 33 deletions src/parse.nr
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<N> 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;
Expand All @@ -173,12 +173,11 @@ impl<N> 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;
Expand All @@ -199,15 +198,15 @@ impl<N> 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))
{
OK = value;

if (byte == BEGIN_ARRAY)
{
OK &= (!array_value | (array_value & value_delimiter));

prop.value = prop.value.push_back(byte);
array_value = array;

Expand All @@ -224,8 +223,6 @@ impl<N> 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);
Expand All @@ -246,10 +243,10 @@ impl<N> 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;
Expand All @@ -258,10 +255,10 @@ impl<N> 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)
{
Expand All @@ -275,12 +272,10 @@ impl<N> 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;
}
Expand All @@ -298,7 +293,6 @@ impl<N> ByteArrayParser for [u8; N]
{
OK = ((object | array) & array_value);

array_value = false;
key_delimiter = false;
value_delimiter = true;
done = (OK & !array);
Expand All @@ -308,9 +302,9 @@ impl<N> 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;
Expand All @@ -323,9 +317,12 @@ impl<N> 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;
Expand All @@ -346,10 +343,9 @@ impl<N> 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)
Expand All @@ -366,21 +362,18 @@ impl<N> 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)
{
Expand All @@ -393,7 +386,6 @@ impl<N> ByteArrayParser for [u8; N]
value = false;
number = false;
literal = false;
minus = false;
digit_0 = false;
digit_1_9 = false;
fraction = false;
Expand All @@ -403,18 +395,16 @@ impl<N> 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]; }
}
Expand Down

0 comments on commit 7419e1b

Please sign in to comment.