Skip to content

Commit

Permalink
added wordCount method
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Downs <[email protected]>
  • Loading branch information
briandowns committed Jan 8, 2024
1 parent 0b96d02 commit 2d75174
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/docs/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,15 @@ Returns a boolean indicating that the given string is an lower case letter.
"D".isLower(); // false
"d".isLower() // true
"Dog".isLower() // false
```
```

### string.wordCount() -> Number

Returns the number of words in the given string.

```cs
"".wordCount(); // 0
"This".wordCount(); // 1
"This is a sentence".wordCount(); // 4
"This is an even longer sentence".wordCount(); // 6
```
27 changes: 27 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 @@ -657,6 +683,7 @@ 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);
Expand Down
1 change: 1 addition & 0 deletions tests/strings/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ import "repeat.du";
import "title.du";
import "isUpper.du";
import "isLower.du";
import "wordCount.du";
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();

0 comments on commit 2d75174

Please sign in to comment.