Skip to content

Instantly share code, notes, and snippets.

@water-air-flash
Created September 23, 2016 08:31
Show Gist options
  • Select an option

  • Save water-air-flash/fb74ab18c53c485769c2a6cd1b81f082 to your computer and use it in GitHub Desktop.

Select an option

Save water-air-flash/fb74ab18c53c485769c2a6cd1b81f082 to your computer and use it in GitHub Desktop.

Revisions

  1. @Kangaroux Kangaroux revised this gist May 4, 2014. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion string.c
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ void str_shrink(string *s) {
    s->str[s->size-1] = '\0';
    }

    unsigned int str_size(string *s) {
    unsigned int str_sizeof(string *s) {
    return s->size;
    }

    2 changes: 1 addition & 1 deletion string.h
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ void str_grow(string *s);
    void str_growto(string *s, unsigned int newsize);
    void str_shrink(string *s);

    unsigned int str_size(string *s);
    unsigned int str_sizeof(string *s);
    unsigned int str_length(string *s);

    void str_append(string *s, char *app);
  2. @Kangaroux Kangaroux revised this gist May 2, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion string.c
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,9 @@ void str_growto(string *s, unsigned int newsize) {

    unsigned int oldsize = s->size;

    s->size = newsize;
    while(s->size < newsize)
    s->size *= 2;

    s->str = (char *)realloc((void *)s->str, s->size);

    memset((void *)(s->str + oldsize), '\0', s->size - oldsize);
  3. @Kangaroux Kangaroux created this gist Apr 29, 2014.
    32 changes: 32 additions & 0 deletions main.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #include <stdlib.h>
    #include <stdio.h>
    #include "string.h"

    string *readline() {
    string *buf = str_create("");
    int c;

    for(c = getchar(); c != EOF && c != '\n'; c = getchar()) {
    str_cappend(buf, c);
    }

    return buf;
    }

    int main() {
    string *mystring = readline();

    // example std input: "this is an example"

    printf("%s\n", str_get(mystring)); // prints: this is an example
    printf("%d\n", str_length(mystring)); // prints: 18
    printf("%c\n", str_getc(mystring, 5)); // prints: 'i'

    strncpy(mystring->str, "here", strlen("here"));

    printf("%s\n", str_get(mystring)); // prints: here is an example

    str_free(mystring);

    return 0;
    }
    86 changes: 86 additions & 0 deletions string.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    #include "string.h"

    #define MAX(a, b) ((a) > (b) ? (a) : (b))

    string *str_create(char *init) {
    string *s = (string *)malloc(sizeof(string));

    if(!s) return NULL;

    s->size = MAX(strlen(init) + 1, STRSIZE);
    s->str = (char *)malloc(s->size);

    strcpy(s->str, init);

    return s;
    }

    void str_free(string *s) {
    free(s->str);
    free(s);
    }

    void str_growto(string *s, unsigned int newsize) {
    if(newsize <= s->size) return;

    unsigned int oldsize = s->size;

    s->size = newsize;
    s->str = (char *)realloc((void *)s->str, s->size);

    memset((void *)(s->str + oldsize), '\0', s->size - oldsize);
    }

    void str_grow(string *s) {
    str_growto(s, s->size * 2);
    }

    void str_shrink(string *s) {
    s->size /= 2;
    s->size = MAX(s->size, STRSIZE);
    s->str = (char *)realloc((void *)s->str, s->size);
    s->str[s->size-1] = '\0';
    }

    unsigned int str_size(string *s) {
    return s->size;
    }

    unsigned int str_length(string *s) {
    return strlen(s->str);
    }

    char str_getc(string *s, unsigned int index) {
    if(index >= s->size) return '\0';

    return s->str[index];
    }

    char *str_get(string *s) {
    return s->str;
    }

    void str_append(string *s, char *app) {
    int len = strlen(s->str);

    str_growto(s, s->size + len);

    int i;
    for(i = 0; i < strlen(app); i++) {
    s->str[len+i] = app[i];
    }
    }

    void str_cappend(string *s, char c) {
    unsigned int len = strlen(s->str);

    if(len >= s->size-1)
    str_grow(s);

    s->str[len] = c;
    }

    void str_set(string *s, char *newstr) {
    str_growto(s, s->size + strlen(newstr));
    strcpy(s->str, newstr);
    }
    32 changes: 32 additions & 0 deletions string.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #ifndef STRING_H
    #define STRING_H

    #include <stdlib.h>
    #include <string.h>

    #define STRSIZE 10

    typedef struct {
    char *str;
    unsigned int size;
    } string;

    string *str_create(char *init);
    void str_free(string *s);

    void str_grow(string *s);
    void str_growto(string *s, unsigned int newsize);
    void str_shrink(string *s);

    unsigned int str_size(string *s);
    unsigned int str_length(string *s);

    void str_append(string *s, char *app);
    void str_cappend(string *s, char c);

    char str_getc(string *s, unsigned int index);
    char *str_get(string *s);

    void str_set(string *s, char *newstr);

    #endif