Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): string utility module #13

Merged
merged 7 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ We welcome contributions from the community! If you'd like to contribute, please
<br />
<sub><b>Kay</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/olumo-oke">
<img src="https://avatars.githubusercontent.com/u/173970179?v=4" width="50;" alt="olumo-oke"/>
<br />
<sub><b>olumo-oke</b></sub>
</a>
</td>
</tr>
<tbody>
Expand Down
81 changes: 81 additions & 0 deletions contracts/traits/utils/string.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// String module with String Comparison, Trimming, Padding, and other string operations

// Function to concatenate two strings
fun concatenate(str1: String, str2: String): String {
return str1 ++ str2;
}

// Function to extract a substring
fun substring(str: String, start: Int, length: Int): String {
return str.sub(start, length);
}

// Function to get the length of a string
fun length(str: String): Int {
return str.len();
}

// Function to compare two strings lexicographically
fun compare(str1: String, str2: String): Int {
if (str1 == str2) {
return 0;
} else if (str1 < str2) {
return -1;
} else {
return 1;
}
}

// Function to convert an integer to a string
fun intToString(num: Int): String {
return num.toStr();
}

// Function to access a character in a string at a specific index
fun charAt(str: String, index: Int): String {
require(index >= 0 && index < str.len(), "Index out of bounds");
return str.sub(index, 1);
}

// Function to check if a string contains a specific substring using regular expressions
fun contains(str: String, pattern: String): Bool {
let regex = pattern.toRegexp();
return regex.match(str) != null;
}

// Function to trim whitespace from the beginning and end of a string
fun trim(str: String): String {
return str.trim();
}

// Function to pad a string with specified characters on the left
fun padLeft(str: String, length: Int, char: String): String {
let diff = length - str.len();
if (diff <= 0) {
return str;
}
return char.repeat(diff) ++ str;
}

// Function to pad a string with specified characters on the right
fun padRight(str: String, length: Int, char: String): String {
let diff = length - str.len();
if (diff <= 0) {
return str;
}
return str ++ char.repeat(diff);
}

// Example usage
ZigBalthazar marked this conversation as resolved.
Show resolved Hide resolved
// let concatenated = concatenate("Hello, ", "World!");
// let sub = substring("Hello, World!", 7, 5);
// let len = length("Hello");
// let cmp = compare("abc", "def");
// let str = intToString(123);
// let char = charAt("Hello", 2);
// let containsPattern = contains("Hello, World!", "World");
// let trimmed = trim(" Hello, World! ");
// let paddedLeft = padLeft("Hello", 10, "*");
// let paddedRight = padRight("Hello", 10, "*");

// Unicode support is inherent in Tact's string operations, ensuring compatibility with different characters and symbols.
9 changes: 9 additions & 0 deletions wrappers/stringutil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CompilerConfig } from '@ton/blueprint';

export const compile: CompilerConfig = {
lang: 'tact',
target: 'contracts\traits\string.tact',
options: {
debug: true,
},
};
Loading