Skip to content

Commit

Permalink
[Feature] Add ASCII module and TODO: ascii DAO name (#161)
Browse files Browse the repository at this point in the history
* add ASCII module
  • Loading branch information
WGB5445 authored Sep 27, 2022
1 parent 1fb3b8a commit d3eb047
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 12 deletions.
5 changes: 4 additions & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ compiled_package_info:
StarcoinFramework: "0x00000000000000000000000000000001"
VMReserved: "0x00000000000000000000000000000000"
module_resolution_metadata:
? address: "0x00000000000000000000000000000001"
name: ASCII
: StarcoinFramework
? address: "0x00000000000000000000000000000001"
name: Account
: StarcoinFramework
Expand Down Expand Up @@ -318,7 +321,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: A155383ABA4B2CECDADAD666C4E012F372A73E64B9B0EA9CA977261219D8ED8E
source_digest: 4C96D09C5AAF229BF0BFA79CEEEC3E42D49C436FEBA79BBF776354A60A84F1BD
build_flags:
dev_mode: false
test_mode: false
Expand Down
1 change: 1 addition & 0 deletions build/StarcoinFramework/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This is the root document for the Move StarcoinFramework module documentation. T
## Index


- [`0x1::ASCII`](ASCII.md#0x1_ASCII)
- [`0x1::Account`](Account.md#0x1_Account)
- [`0x1::AccountScripts`](AccountScripts.md#0x1_AccountScripts)
- [`0x1::AnyMemberPlugin`](AnyMemberPlugin.md#0x1_AnyMemberPlugin)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_ext.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 9 tasks

task 6 'run'. lines 56-65:
{
"gas_used": 964604,
"gas_used": 1008684,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_grant.exp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ task 5 'run'. lines 13-21:

task 8 'run'. lines 123-134:
{
"gas_used": 1768185,
"gas_used": 1812265,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_proposal.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 42 tasks

task 7 'run'. lines 169-180:
{
"gas_used": 1765388,
"gas_used": 1809468,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_proposal_rejected.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 40 tasks

task 7 'run'. lines 126-135:
{
"gas_used": 1168754,
"gas_used": 1212834,
"status": {
"MoveAbort": {
"location": {
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_upgrade.exp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ task 9 'run'. lines 130-144:

task 10 'run'. lines 145-154:
{
"gas_used": 1051127,
"gas_used": 1095207,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/dao_upgrade_incompatible.exp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ task 8 'run'. lines 121-136:

task 9 'run'. lines 138-147:
{
"gas_used": 1051127,
"gas_used": 1095207,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/stake_to_sbt_plugin.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 17 tasks

task 5 'run'. lines 71-79:
{
"gas_used": 1714614,
"gas_used": 1758694,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospace/xdao.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 7 tasks

task 3 'run'. lines 54-61:
{
"gas_used": 1124190,
"gas_used": 1168270,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospaceplugin/mint_proposal_plugin.exp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ task 8 'run'. lines 71-85:

task 9 'run'. lines 87-96:
{
"gas_used": 1189126,
"gas_used": 1233206,
"status": "Executed"
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/daospaceplugin/salary_gov_plugin.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 21 tasks

task 7 'run'. lines 252-264:
{
"gas_used": 1723350,
"gas_used": 1767430,
"status": "Executed"
}

Expand Down
138 changes: 138 additions & 0 deletions sources/ASCII.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
module StarcoinFramework::ASCII {
use StarcoinFramework::Vector;
use StarcoinFramework::Errors;
use StarcoinFramework::Option::{Self, Option};

/// An invalid ASCII character was encountered when creating an ASCII string.
const EINVALID_ASCII_CHARACTER: u64 = 101;

/// The `String` struct holds a vector of bytes that all represent
/// valid ASCII characters. Note that these ASCII characters may not all
/// be printable. To determine if a `String` contains only "printable"
/// characters you should use the `all_characters_printable` predicate
/// defined in this module.
struct String has copy, drop, store {
bytes: vector<u8>,
}


/// An ASCII character.
struct Char has copy, drop, store {
byte: u8,
}

/// Convert a `byte` into a `Char` that is checked to make sure it is valid ASCII.
public fun char(byte: u8): Char {
assert!(is_valid_char(byte), Errors::invalid_argument(EINVALID_ASCII_CHARACTER));
Char { byte }
}


/// Convert a vector of bytes `bytes` into an `String`. Aborts if
/// `bytes` contains non-ASCII characters.
public fun string(bytes: vector<u8>): String {
let x = try_string(bytes);
assert!(
Option::is_some(&x),
Errors::invalid_argument(EINVALID_ASCII_CHARACTER)
);
Option::destroy_some(x)
}

/// Convert a vector of bytes `bytes` into an `String`. Returns
/// `Some(<ascii_string>)` if the `bytes` contains all valid ASCII
/// characters. Otherwise returns `None`.
public fun try_string(bytes: vector<u8>): Option<String> {
let len = Vector::length(&bytes);
let i = 0;
while ( i < len ) {
let possible_byte = *Vector::borrow(&bytes, i);
if (!is_valid_char(possible_byte)) return Option::none();
i = i + 1;
};
Option::some(String { bytes })
}

/// Returns `true` if all characters in `string` are printable characters
/// Returns `false` otherwise. Not all `String`s are printable strings.
public fun all_characters_printable(string: &String): bool {
let len = Vector::length(&string.bytes);
let i = 0;
while ( i < len ) {
let byte = *Vector::borrow(&string.bytes, i);
if (!is_printable_char(byte)) return false;
i = i + 1;
};
true
}

public fun push_char(string: &mut String, char: Char) {
Vector::push_back(&mut string.bytes, char.byte);
}


public fun pop_char(string: &mut String): Char {
Char { byte: Vector::pop_back(&mut string.bytes) }
}


public fun length(string: &String): u64 {
Vector::length(as_bytes(string))
}

/// Get the inner bytes of the `string` as a reference
public fun as_bytes(string: &String): &vector<u8> {
&string.bytes
}

/// Unpack the `string` to get its backing bytes
public fun into_bytes(string: String): vector<u8> {
let String { bytes } = string;
bytes
}

/// Unpack the `char` into its underlying byte.
public fun byte(char: Char): u8 {
let Char { byte } = char;
byte
}

/// Returns `true` if `byte` is a valid ASCII character. Returns `false` otherwise.
public fun is_valid_char(byte: u8): bool {
byte <= 0x7F
}

/// Returns `true` if `byte` is an printable ASCII character. Returns `false` otherwise.
public fun is_printable_char(byte: u8): bool {
byte >= 0x20 && // Disallow metacharacters
byte <= 0x7E // Don't allow DEL metacharacter
}

/// split string by char. Returns vector<String>
public fun split_by_char(string: String, char: Char): vector<String> {
let result = Vector::empty<String>();
let len = length(&string);
let i = 0;
let buffer = Vector::empty<u8>();
while ( i < len ) {
let byte = *Vector::borrow(&string.bytes, i);
if (byte != char.byte) {
Vector::push_back(&mut buffer, byte);
} else {
Vector::push_back(&mut result, string(buffer));
buffer = Vector::empty<u8>();
if (i != 0 && i == len - 1) {
// special
Vector::push_back(&mut result, string(copy buffer));
};
};

i = i + 1;
};

if (len == 0 || Vector::length(&buffer) != 0) {
Vector::push_back(&mut result, string(buffer));
};
result
}
}
3 changes: 2 additions & 1 deletion sources/daospace/DAOSpace.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module StarcoinFramework::DAOSpace {
use StarcoinFramework::Block;
use StarcoinFramework::DAOPluginMarketplace;
use StarcoinFramework::EventUtil;
use StarcoinFramework::ASCII;

friend StarcoinFramework::StarcoinDAO;

Expand Down Expand Up @@ -77,7 +78,6 @@ module StarcoinFramework::DAOSpace {
/// DAO resource, every DAO has this resource at it's DAO account
struct DAO has key {
id: u64,
// TODO migrate ASIIC String and use ASSIC String
name: vector<u8>,
// description ipfs://xxxxx
description:vector<u8>,
Expand Down Expand Up @@ -269,6 +269,7 @@ module StarcoinFramework::DAOSpace {

let dao_address = Signer::address_of(&dao_signer);
let id = DAORegistry::register<DAOT>(dao_address);
ASCII::string(copy name);
let dao = DAO{
id,
name: copy name,
Expand Down

0 comments on commit d3eb047

Please sign in to comment.