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

Added IsLower and isUpper and wordCount string methods #724

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 34 additions & 1 deletion docs/docs/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,37 @@ Returns a new string with the original string repeated the given number of times
```cs
"ba"+"na".repeat(2); // banana
"la".repeat(2) + " land"; // lala land
```
```

### string.isUpper() -> Boolean

Returns a boolean indicating that the given string is an upper case letter.

briandowns marked this conversation as resolved.
Show resolved Hide resolved
```cs
"D".isUpper(); // true
"d".isUpper() // false
"G00D!".isUpper() // true
"Dog".isUpper() // false
```

### string.isLower() -> Boolean

Returns a boolean indicating that the given string is an lower case letter. Empty strings are considered false.

```cs
"D".isLower(); // false
"d".isLower() // true
"g00d!".isLower() // true
"Dog".isLower() // false
```

### string.wordCount() -> Number

Returns the number of words in the given string. Empty strings are considered false.

```cs
"".wordCount(); // 0
"This".wordCount(); // 1
"This is a sentence".wordCount(); // 4
"This is an even longer sentence".wordCount(); // 6
```
73 changes: 73 additions & 0 deletions src/vm/datatypes/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,32 @@ static Value countString(DictuVM *vm, int argCount, Value *args) {
return NUMBER_VAL(count);
}

static Value wordCountString(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "count() takes no arguments (%d given)", argCount);
return EMPTY_VAL;
}

char *string = AS_CSTRING(args[0]);

int count = 0;
int len = strlen(string);
bool in = false;

for (int i = 0; i < len; i++) {
if (isspace(string[i])) {
in = false;
} else if(isalpha(string[i])) {
if(!in) {
in = true;
count++;
}
}
}

return NUMBER_VAL(count);
}

static Value titleString(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "title() takes no arguments (%d given)", argCount);
Expand Down Expand Up @@ -598,6 +624,50 @@ static Value repeatString(DictuVM *vm, int argCount, Value *args) {
return OBJ_VAL(takeString(vm, temp, tempLen - 1));
}

static Value isUpperString(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "isUpper() takes no arguments (%d given)", argCount);
return EMPTY_VAL;
}

char *string = AS_CSTRING(args[0]);
int len = strlen(string);

if (len == 0) {
return BOOL_VAL(false);
}

for (int i = 0; i < len; i++) {
if (!isupper(string[i]) && isalpha(string[i])) {
return BOOL_VAL(false);
}
}

return BOOL_VAL(true);
}

static Value isLowerString(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "isLower() takes no arguments (%d given)", argCount);
return EMPTY_VAL;
}

char *string = AS_CSTRING(args[0]);
int len = strlen(string);

if (len == 0) {
return BOOL_VAL(false);
}

for (int i = 0; i < len; i++) {
if (!islower(string[i]) && isalpha(string[i])) {
return BOOL_VAL(false);
}
}

return BOOL_VAL(true);
}

void declareStringMethods(DictuVM *vm) {
defineNative(vm, &vm->stringMethods, "len", lenString);
defineNative(vm, &vm->stringMethods, "toNumber", toNumberString);
Expand All @@ -615,8 +685,11 @@ void declareStringMethods(DictuVM *vm) {
defineNative(vm, &vm->stringMethods, "rightStrip", rightStripString);
defineNative(vm, &vm->stringMethods, "strip", stripString);
defineNative(vm, &vm->stringMethods, "count", countString);
defineNative(vm, &vm->stringMethods, "wordCount", wordCountString);
defineNative(vm, &vm->stringMethods, "toBool", boolNative); // Defined in util
defineNative(vm, &vm->stringMethods, "title", titleString);
defineNative(vm, &vm->stringMethods, "repeat", repeatString);
defineNative(vm, &vm->stringMethods, "isUpper", isUpperString);
defineNative(vm, &vm->stringMethods, "isLower", isLowerString);

}
3 changes: 3 additions & 0 deletions tests/strings/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ import "toBool.du";
import "escapeCodes.du";
import "repeat.du";
import "title.du";
import "isUpper.du";
import "isLower.du";
import "wordCount.du";
21 changes: 21 additions & 0 deletions tests/strings/isLower.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* isLower.du
*
* Testing the str.isLower() method
*
* .isLower() returns a boolean indicating that the given string is
* an lower case letter.
*/
from UnitTest import UnitTest;

class TestStringIsLower < UnitTest {
testStringIsLower() {
this.assertTruthy("d".isLower());
this.assertTruthy("dog".isLower());
this.assertTruthy("g00d!".isLower());
this.assertFalsey("D".isLower());
this.assertFalsey("Maple".isLower());
}
}

TestStringIsLower().run();
20 changes: 20 additions & 0 deletions tests/strings/isUpper.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* isUpper.du
*
* Testing the str.isUpper() method
*
* .isUpper() returns a boolean indicating that the given string is
* an upper case letter.
*/
from UnitTest import UnitTest;

class TestStringIsUpper < UnitTest {
testStringIsUpper() {
this.assertTruthy("D".isUpper());
this.assertTruthy("DOG".isUpper());
this.assertTruthy("G00D!".isUpper());
this.assertFalsey("Maple".isUpper());
}
}

TestStringIsUpper().run();
19 changes: 19 additions & 0 deletions tests/strings/wordCount.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* wordCount.du
*
* Testing the str.wordCount() method
*
* .wordCount() returns the number of words in the given string.
*/
from UnitTest import UnitTest;

class TestStringWordCount < UnitTest {
testStringWordCount() {
this.assertEquals("".wordCount(), 0);
this.assertEquals("This".wordCount(), 1);
this.assertEquals("This is a sentence".wordCount(), 4);
this.assertEquals("This is an even longer sentence".wordCount(), 6);
}
}

TestStringWordCount().run();