Skip to content

Documentation

Lenny edited this page Feb 24, 2023 · 16 revisions

Go Syntax to C

Clone

Clone system is a bit different, the clone system will hold onto the pointer and de-allocate it at clone_collect() allowing for easier char* memory management.

#include "gostrings.h"

int main() {

    char *str1 = Clone("hello world");
    char *str2 = Clone(str1);
    printf("%s\n", str1); //hello world
    printf("%s\n", str2); //hello world

    clone_collect(); //this de-allocates all the clones

    return 0;
}

Compare

#include "gostrings.h"

int main() {
  char str1[] = "Hello";
  char str2[] = "World";
  int result = compare(str1, str2);
  printf("%d\n", result);
  return 0;
}

Contains

#include "gostrings.h"

int main() {
    const char* str = "hello world";
    const char* substr = "world";
    if (contains(str, substr)) {
        printf("'%s' contains '%s'\n", str, substr);
    } else {
        printf("'%s' does not contain '%s'\n", str, substr);
    }
    return 0;
}

//Outputs 'hello world' contains 'world'

ContainsAny

n.a

ContainsRunes

Contains cans be used the same way

int main() {
    const char* str = "ᛈᚢᛒᛚᛁᚲ";
    const char* substr = "ᚲ";
    if (contains(str, substr)) {
        printf("'%s' contains '%s'\n", str, substr);
    } else {
        printf("'%s' does not contain '%s'\n", str, substr);
    }
    return 0;
}```

## Count
```C
#include "gostrings.h"

int main() {
    const char s1[] = "Hello World, World!";
    const char s2[] = "World";

    int count = go_count(s1, s2);
    printf("count of \"%s\" in \"%s\": %d\n", s2, s1, count); // prints "count of "World" in "Hello World, World!": 2"

    return 0;
}

Cut

#include "gostrings.h"

int main() {
  char str[] = "Hello,World,Goodbye";
  char sep[] = ",";
  char *result = cut(str, sep);
  printf("%s\n", result); // -> Hello
  free(result);
  return 0;
}

CutPrefix

#include "gostrings.h"

int main() {
  char str[] = "Hello,World";
  char suffix[] = ",World";
  char prefix[] = "Hello,";
  char *result_suffix = cut_suffix(str, suffix); 
  char *result_prefix = cut_prefix(str, prefix);
  printf("%s\n", result_suffix); //Outputs "Hello"
  printf("%s\n", result_prefix); //Outputs "World"
  free(result_suffix);
  free(result_prefix);
  return 0;
}

CutSuffix

#include "gostrings.h"

int main() {
  char str[] = "Hello,World";
  char suffix[] = ",World";
  char prefix[] = "Hello,";
  char *result_suffix = cut_suffix(str, suffix); 
  char *result_prefix = cut_prefix(str, prefix);
  printf("%s\n", result_suffix); //Outputs "Hello"
  printf("%s\n", result_prefix); //Outputs "World"
  free(result_suffix);
  free(result_prefix);
  return 0;
}

Equalsfold

#include "gostrings.h"

int main() {
    const char s1[] = "Hello";
    const char s2[] = "hello";
    const char s3[] = "world";

    printf("%d\n", equalFold(s1, s2)); // prints 1 (true)
    printf("%d\n", equalFold(s1, s3)); // prints 0 (false)

    return 0;
}

Fields

#include "gostrings.h"

int main() {
    char str[] = " hello   world\n  ";
    int count = 0;

    char **result = fields(str, &count);

    for (int i = 0; i < count; i++) {
        printf("%s\n", result[i]);
        free(result[i]);
    }
    free(result);

    return 0;
}
//outputs Hello
//World

FieldsFunc

n.a

HasPrefix

#include "gostrings.h"


int main() {
    const char* str = "hello world";
    const char* prefix = "hello";
    if (has_prefix(str, prefix)) {
        printf("'%s' has prefix '%s'\n", str, prefix);
    } else {
        printf("'%s' does not have prefix '%s'\n", str, prefix);
    }
    return 0;
}

//Outputs True

HasSuffix

#include "gostrings.h"


int main() {
    const char* str = "hello world";
    const char* suffix = "world";
    if (has_suffix(str, suffix)) {
        printf("'%s' has suffix '%s'\n", str, suffix);
    } else {
        printf("'%s' does not have suffix '%s'\n", str, suffix);
    }
    return 0;
}
//In this case, it's "true" because char "world" is the suffix of char "hello world"

Index

#include "gostrings.h"

int main() {
    char str[] = "hello world, world";
    char substr[] = "world";
    int index = index_of(str, substr);
    printf("Index of '%s' in '%s': %d\n", substr, str, index);
    return 0;
}

//Output: Index of 'world' in 'hello world and world': 6

LastIndex

#include "gostrings.h"

int main() {
    char str[] = "hello world";
    char substr[] = "o";
    int index = last_index_of(str, substr);
    printf("Last index of '%s' in '%s': %d\n", substr, str, index);
    return 0;
}

Map

n.a

Repeat

#include "gostrings.h"

int main() {
    char *s = "hello ";
    int count = 3;
    char *result = repeat(s, count);
    if (!result) {
        fprintf(stderr, "Error: out of memory\n");
        return 1;
    }
    printf("Result: %s\n", result); // Result: hello hello hello 
    free(result);
    return 0;
}

Replace

#include "gostrings.h"

int main() {
    char *s = "hello,world!";
    char *old = ",";
    char *new = " and ";
    char *result = replace(s, old, new);
    if (result != NULL) {
        printf("%s\n", result); //hello and world!
        free(result);
    } else {
        fprintf(stderr, "Error: substring not found\n");
        return 1;
    }
    return 0;
}

ReplaceAll

#include "gostrings.h"

int main() {
    char str1[] = "Hello World, World!";
    const char old1[] = "World";
    const char new1[] = "Earth";
    printf("%s\n", replaceall(str1, old1, new1)); // prints "Hello Earth, World!"
    return 0;
}

// prints "Hello Earth, World!"

Split

#include "gostrings.h"

int main() {
    char str[] = "Hello,World";
    char *sep = ",";
    int count = 0;

    char **result = split(str, sep, &count);

    for (int i = 0; i < count; i++) {
        printf("%d -> %s\n", i, result[i]);
        free(result[i]);
    }
    free(result);

    return 0;
}
//Output:
//0 -> Hello
//1 -> World

SplitAfter

#include "gostrings.h"

int main() {
    const char *str = "hello,world,how,are,you";
    const char *sep = ",";
    int count;
    char **result = split_after(str, sep, &count);

    for (int i = 0; i < count; i++) {
        printf("%s\n", result[i]);
        free(result[i]);
    }
    free(result);
    return 0;
}

/*Output:
hello,
world,
how,
are,
you
*/

SplitAfterN

#include "gostrings.h"

int main(void) {
    const char *str = "hello,world,how,are,you";
    int count;
    char **result = split_after_n(str, ",", 3, &count);
    for (int i = 0; i < count; i++) {
        printf("%s\n", result[i]);
        free(result[i]);
    }
    free(result);
    return 0;
}

/*output
hello,world,how,
are,
you*/

SplitN

int main() {
    const char *str = "hello,world,how,are,you";
    const char *sep = ",";
    int max_splits = 3;
    int count;
    char **result = split_n(str, sep, max_splits, &count);
    for (int i = 0; i < count; i++) {
        printf("%s\n", result[i]);
        free(result[i]);
    }
    free(result);
    return 0;
}

Join

#include "gostrings.h"

int main() {
    const char* strings[] = {"apple", "orange", "banana", "grape"};
    char* result = join(strings, ", ");
    printf("Result: %s\n", result);
    free(result);
    
    return 0;
}

//Result: apple, orange, banana, grape

ToLower / ToUpper

#include "gostrings.h"

int main() {
    char *s = "Hello, World!";
    char *lower = toLower(s);
    char *upper = toUpper(s);
    printf("%s\n", lower); //hello, world!
    printf("%s\n", upper); //HELLO, WORLD!
    free(lower);
    free(upper);
    return 0;
}

ToTitle

#include "gostrings.h"

int main() {
    char *s = "the quick brown fox jumps over the lazy dog";
    char *title = toTitle(s); //The Quick Brown Fox Jumps Over The Lazy Dog
    printf("%s\n", title);
    free(title);
    return 0;
}

Trim

#include "gostrings.h"

int main() {
  char str[] = "@@Example of trimming$$";
  char chars[] = "@$";
  char *result = trim(str, chars);
  printf("%s\n", result); //Example of trimming
  free(result);
  return 0;
}