Skip to content

Commit

Permalink
Add trim method to string_rt
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Jan 11, 2025
1 parent 61acdbb commit 7c1d9b5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
13 changes: 4 additions & 9 deletions media/test-project/test.spice
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
type T dyn;

f<int> print<T>(T g) {
printf("%d", g);
return 0;
}

f<int> main() {
print(1);
print("string");
String test = String("String to be trimmed ");
printf("'%s'\n", test);
String trimmed = test.trim();
printf("'%s'\n", trimmed);
}
5 changes: 1 addition & 4 deletions src-bootstrap/util/common-util.spice
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import "bootstrap/source-file-intf";
*/
public f<String> getLastFragment(const String &haystack, const string needle) {
const unsigned long index = haystack.rfind(needle);
if index == -1l {
return haystack;
}
return haystack.getSubstring(index + getRawLength(needle));
return index != -1l ? haystack.getSubstring(index + getRawLength(needle)) : haystack;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/util/CommonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ std::string CommonUtil::getLastFragment(const std::string &haystack, const std::
*/
std::string CommonUtil::trim(const std::string &input) {
const size_t first = input.find_first_not_of(' ');
if (std::string::npos == first)
if (first == std::string::npos)
return input;
const size_t last = input.find_last_not_of(' ');
return input.substr(first, (last - first + 1));
const size_t newLength = last - first + 1;
return input.substr(first, newLength);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions std/runtime/string_rt.spice
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![core.compiler.alwaysKeepOnNameCollision = true]

// Std imports
import "std/text/analysis";

// Link external functions
// We intentionally do not use the memory_rt here to avoid dependency circles
ext f<heap char*> malloc(unsigned long);
Expand Down Expand Up @@ -604,6 +607,29 @@ public f<String> String.getSubstring<IntLongShort>(unsigned IntLongShort startId
return substring;
}

/**
* Returns a new string without leading or trailing whitespaces.
*
* @return Trimmed string
*/
public f<String> String.trim() {
if this.length == 0l {
return String();
}
unsigned long startIdx = 0l;
unsigned long endIdx = this.length - 1l;

unsafe {
// Find first char that is not a whitespace
while isWhitespace(this.contents[startIdx]) { startIdx++; }
// Find last char that is not a whitespace
while isWhitespace(this.contents[endIdx]) { endIdx--; }
}

const unsigned long newLength = endIdx - startIdx + 1;
return this.getSubstring(startIdx, newLength);
}

/**
* Reserves `charCount` items
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ f<int> main() {
dyn s2 = String("Hello");
dyn s3 = String("Hello!");
dyn s4 = String("Hello World!");
dyn s5 = String(" \n\tString to be trimmed \r ");

assert s1.isEmpty();
assert !s2.isEmpty();
Expand Down Expand Up @@ -40,6 +41,8 @@ f<int> main() {
assert !s4.startsWith("World");
assert s4.endsWith("!");
assert !s2.endsWith("!");
assert s5.trim() == "String to be trimmed";
assert s1.trim() == "";

printf("All assertions passed!");
}

0 comments on commit 7c1d9b5

Please sign in to comment.