Sfoglia il codice sorgente

Adding solution for 2788

Vinicius Teshima 1 mese fa
parent
commit
2126c528c5
2 ha cambiato i file con 95 aggiunte e 0 eliminazioni
  1. 88 0
      C/2788.c
  2. 7 0
      C/build.sh

+ 88 - 0
C/2788.c

@@ -0,0 +1,88 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/**
+ * Note: The returned array must be malloced, assume caller calls free().
+ */
+char**
+splitWordsBySeparator(char** words, int wordsSize, char separator,
+                      int* returnSize)
+{
+    struct {
+        char **data;
+        unsigned long cap;
+        unsigned long size;
+    } ret = {0};
+
+    ret.cap = wordsSize;
+    ret.data = calloc(ret.cap, sizeof(*ret.data));
+
+    for ( int i = 0; i < wordsSize; ++i ) {
+        char *cstr = words[i];
+        unsigned long wsize = 0;
+        unsigned long wcap = 32;
+        while ( *cstr != '\0' ) {
+            if ( *cstr == separator ) {
+                if ( wsize > 0 ) {
+                    ret.data[ret.size][wsize++] = 0;
+                    ++ret.size;
+                }
+                wsize = 0;
+                wcap = 32;
+                if ( ret.size >= ret.cap ) {
+                    ret.cap *= 2;
+                    ret.data = realloc(ret.data, sizeof(*ret.data) * ret.cap);
+                    for ( unsigned long j = ret.size; j < ret.cap; ++j ) {
+                        ret.data[j] = NULL;
+                    }
+                }
+                ++cstr;
+                continue;
+            }
+            if ( ret.data[ret.size] == NULL ) { ret.data[ret.size] = malloc(wcap); }
+            ret.data[ret.size][wsize++] = *cstr;
+            if ( wsize >= wcap ) {
+                wcap *= 2;
+                ret.data[ret.size] = realloc(ret.data[ret.size], wcap);
+            }
+            ++cstr;
+        }
+        if ( wsize != 0 ) {
+            ret.data[ret.size++][wsize++] = 0;
+            if ( ret.size >= ret.cap ) {
+                ret.cap *= 2;
+                ret.data = realloc(ret.data, sizeof(*ret.data) * ret.cap);
+                for ( unsigned long j = ret.size; j < ret.cap; ++j ) {
+                    ret.data[j] = NULL;
+                }
+            }
+        }
+    }
+
+    *returnSize = ret.size;
+    return ret.data;
+}
+
+void
+r(char **ws, int s, char sep)
+{
+    int ret_size = 0;
+    char **ret = splitWordsBySeparator(ws, s, sep, &ret_size);
+    printf("RetSize = %d\n", ret_size);
+    if ( ret == NULL ) return;
+
+    printf("Ret = [");
+    while ( *ret != NULL ) {
+        printf("\"%s\", ", *ret++);
+    }
+    printf("]\n");
+}
+
+int
+main(void)
+{
+    char *words[] = {"one.two.three","four.five","six"};
+    r(words, 3, '.');
+    return 0;
+}

+ 7 - 0
C/build.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+
+newest_file="$(find ./ -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1)"
+
+set -x
+gcc -std=c99 -O0 -ggdb -Wall -Wextra -Werror --sanitize=address $newest_file -o /tmp/out || exit 1
+/tmp/out