Skip to content

Commit

Permalink
Rename default parameter for traits to self
Browse files Browse the repository at this point in the history
  • Loading branch information
grasshopper47 committed Nov 20, 2023
1 parent dc5d47d commit 6d5897d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 53 deletions.
72 changes: 36 additions & 36 deletions src/convert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ use crate::Property;

trait FieldConversion
{
fn get_whole(bytes : Self, begin : Field, end : Field) -> Field;
fn get_offsets(bytes : Self) -> [Field; 5];
fn get_whole(self, begin : Field, end : Field) -> Field;
fn get_offsets(self) -> [Field; 5];
}

trait ByteArrayConversions
{
fn as_bool(bytes : Self) -> Option<bool>;
fn as_field(bytes : Self) -> Option<Field>;
fn as_string(bytes : Self) -> [u8];
fn as_list(bytes : Self) -> [[u8]];
fn as_object(bytes : Self, parent : JSON) -> Object;
fn as_json(bytes : Self) -> JSON;
fn as_bool(self) -> Option<bool>;
fn as_field(self) -> Option<Field>;
fn as_string(self) -> [u8];
fn as_list(self) -> [[u8]];
fn as_object(self, parent : JSON) -> Object;
fn as_json(self) -> JSON;
}

trait PropertyLookup
Expand All @@ -57,34 +57,34 @@ trait PropertyConversions
impl<N> FieldConversion for [u8; N]
{
unconstrained
fn get_whole(bytes : Self, begin : Field, end : Field) -> Field
fn get_whole(self, begin : Field, end : Field) -> Field
{
let mut it : Field = (end - 1); // walking in reverse
let mut whole : Field = ((bytes[it] as u4) as Field); // first digit is last in array
let mut whole : Field = ((self[it] as u4) as Field); // first digit is last in array
let mut units : Field = 10;

for _ in begin..it
{
it -= 1;
whole += (((bytes[it] as u4) as Field) * units); // cast ASCII byte to integer digit number
whole += (((self[it] as u4) as Field) * units); // cast ASCII byte to integer digit number
units *= 10;
}

whole
}

unconstrained
fn get_offsets(bytes : Self) -> [Field; 5]
fn get_offsets(self) -> [Field; 5]
{
let mut result : [Field; 5] = [0; 5];

let size : Field = bytes.len();
let size : Field = self.len();
if (size != 0)
{
let start : Field = ((bytes[0] == MINUS) as Field);
let start : Field = ((self[0] == MINUS) as Field);

// ensure at least one digit
let mut valid = if (size != 1) { (bytes[start] - ZERO) < 10 } else { (bytes[0] - ZERO) < 10 };
let mut valid = if (size != 1) { (self[start] - ZERO) < 10 } else { (self[0] - ZERO) < 10 };

if (valid)
{
Expand All @@ -96,7 +96,7 @@ impl<N> FieldConversion for [u8; N]
{
if (valid)
{
let byte = bytes[i];
let byte = self[i];

if (byte == POINT)
{
Expand Down Expand Up @@ -131,9 +131,9 @@ impl<N> FieldConversion for [u8; N]
impl<N> ByteArrayConversions for [u8; N]
{
unconstrained
pub fn as_bool(array : Self) -> Option<bool>
pub fn as_bool(self) -> Option<bool>
{
let bytes : [u8] = array.parse_string();
let bytes : [u8] = self.parse_string();

if (bytes.len() == 0) { Option::none() }
else
Expand All @@ -145,9 +145,9 @@ impl<N> ByteArrayConversions for [u8; N]
}

unconstrained
pub fn as_field(array : Self) -> Option<Field>
pub fn as_field(self) -> Option<Field>
{
let bytes : [u8] = array.parse_string();
let bytes : [u8] = self.parse_string();

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

Expand Down Expand Up @@ -180,27 +180,27 @@ impl<N> ByteArrayConversions for [u8; N]
}

unconstrained
pub fn as_string(array : Self) -> [u8]
pub fn as_string(self) -> [u8]
{
let mut bytes : [u8] = [];

let mut size : Field = array.len();
let mut size : Field = self.len();
if (size != 0)
{
size -= 1;
if (array[0] == QUOTATION_MARK) & (array[size] == QUOTATION_MARK)
if (self[0] == QUOTATION_MARK) & (self[size] == QUOTATION_MARK)
{
for i in 1..size { bytes = bytes.push_back(array[i]); }
for i in 1..size { bytes = bytes.push_back(self[i]); }
}
}

bytes
}

unconstrained
pub fn as_list(array : Self) -> [[u8]]
pub fn as_list(self) -> [[u8]]
{
let bytes : [u8] = array.parse_string();
let bytes : [u8] = self.parse_string();

let mut size : Field = bytes.len();
if (size == 0) | (size == 1) { [] }
Expand Down Expand Up @@ -235,25 +235,25 @@ impl<N> ByteArrayConversions for [u8; N]
}

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

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

if (OK & (size == 3))
{
let index : u8 = bytes[1];
let index : u8 = self[1];
if (index < (parent.children.len() as u8)) { result = Object { parent, doc: parent.children[index] }; }
}
else if (OK & (size != 1)) { result.doc = [crate::Property::none()]; }
Expand All @@ -264,14 +264,14 @@ impl<N> ByteArrayConversions for [u8; N]
}

unconstrained
pub fn as_json(bytes : Self) -> JSON
pub fn as_json(self) -> JSON
{
let mut size : Field = bytes.len();
let mut size : Field = self.len();
if (size == 0) { JSON::none() }
else
{
let mut offset : Field = ((bytes[0] == QUOTATION_MARK) as Field);
bytes.parse(&mut offset, (size - offset), -1)
let mut offset : Field = ((self[0] == QUOTATION_MARK) as Field);
self.parse(&mut offset, (size - offset), -1)
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,48 @@ global chars =

trait ByteArrayEqualityExtension
{
fn eq<N>(bytes : Self, right : [u8; N]) -> bool;
fn less_than_or_eq<N>(bytes : Self, right : [u8; N]) -> (bool, bool);
fn eq<N>(self, other : [u8; N]) -> bool;
fn less_than_or_eq<N>(self, other : [u8; N]) -> (bool, bool);
}

trait ByteArrayExtensions
{
fn eq_string<N>(bytes : Self, right : str<N>) -> bool;
fn eq_string<N>(self, other : str<N>) -> bool;
fn as_array<N>(bytes : [u8; N]) -> Self;
fn print(bytes : Self);
fn print(self);
}

impl<SIZE> ByteArrayEqualityExtension for str<SIZE>
{
unconstrained
pub fn eq<N>(string : Self, right : [u8; N]) -> bool { string.as_bytes().eq(right) }
pub fn eq<N>(self, other : [u8; N]) -> bool { self.as_bytes().eq(other) }

unconstrained
pub fn less_than_or_eq<N>(string : Self, right : [u8; N]) -> (bool, bool) { string.as_bytes().less_than_or_eq(right) }
pub fn less_than_or_eq<N>(self, other : [u8; N]) -> (bool, bool) { self.as_bytes().less_than_or_eq(other) }
}

impl<SIZE> ByteArrayEqualityExtension for [u8; SIZE]
{
unconstrained
pub fn eq<N>(bytes : Self, right : [u8; N]) -> bool
pub fn eq<N>(self, other : [u8; N]) -> bool
{
let size : Field = bytes.len();
let mut result = (size == right.len());
if (result) { for i in 0..size { result &= (bytes[i] == right[i]); } }
let size : Field = self.len();
let mut result = (size == other.len());
if (result) { for i in 0..size { result &= (self[i] == other[i]); } }
result
}

unconstrained
pub fn less_than_or_eq<N>(bytes : Self, right : [u8; N]) -> (bool, bool)
pub fn less_than_or_eq<N>(self, other : [u8; N]) -> (bool, bool)
{
let size_self : Field = bytes.len();
let size_right : Field = right.len();
let size_self : Field = self.len();
let size_right : Field = other.len();

if (size_self == size_right)
{
let mut less_than = false;
let mut equal = true;
for i in 0..size_self { if (equal & (bytes[i] != right[i])) { less_than = (bytes[i] < right[i]); equal = false; } }
for i in 0..size_self { if (equal & (self[i] != other[i])) { less_than = (self[i] < other[i]); equal = false; } }

(less_than, equal)
}
Expand All @@ -64,7 +64,7 @@ impl<SIZE> ByteArrayEqualityExtension for [u8; SIZE]
impl<SIZE> ByteArrayExtensions for [u8; SIZE]
{
unconstrained
pub fn eq_string<N>(bytes : Self, right : str<N>) -> bool { right.eq(bytes) }
pub fn eq_string<N>(self, other : str<N>) -> bool { other.eq(self) }

unconstrained
pub fn as_array<N>(bytes : [u8; N]) -> Self
Expand All @@ -77,10 +77,10 @@ impl<SIZE> ByteArrayExtensions for [u8; SIZE]
}

unconstrained
pub fn print(bytes : Self)
pub fn print(self)
{
dep::std::println("{");
for byte in bytes { let c = chars[byte]; dep::std::println(f" {c}"); }
for byte in self { let c = chars[byte]; dep::std::println(f" {c}"); }
dep::std::println("}");
}
}

0 comments on commit 6d5897d

Please sign in to comment.