Commits

Matt Bone committed 2ec91c2

adding regex thing, inline example

  • Participants

Comments (0)

Files changed (5)

+# use glob syntax.
+syntax: glob
+
+a.out
+*.o
+*.elc
+*.pyc
+*~
+.idea/workspace.xml
+twitter-bootstrap-2.0.4/bootstrap
+#include<stdint.h>
+
+inline
+int uComp0(void const *a, void const *b) {
+  register uint64_t const *const A = a;
+  register uint64_t const *const B = b;
+  {
+    register uint64_t const a = *A;
+    register uint64_t const b = *B;
+    return (a < b) ? -1 : (a > b);
+  }
+}
+
+inline
+int uComp1(void *a, void const *b) {  
+  register uint64_t* A = a;
+  register uint64_t const *const B = b;
+  *A = 1;
+  {
+    register uint64_t const a = *A;
+    register uint64_t const b = *B;
+    return (a < b) ? -1 : (a > b);
+  }
+}
+
+void main() {
+}
+CFLAGS=-g -Wall -O3 -std=gnu11
+#LDLIBS=`pkg-config --libs apophenia glib-2.0`
+
+regex_driver: regex.o
+
+clean:
+	rm *.o
+	rm regex_driver
+/**
+ * Simple regex matcher from chapter 1 of Beautiful Code by Kernighan
+ * and Pike.
+ */
+
+int match(char *regexp, char *text);
+int match_here(char *regexp, char *text);
+int match_star(char c, char *regexp, char *text);
+
+
+int match(char *regexp, char *text) {
+  if (regexp[0] == '^') {
+    return match_here(regexp+1, text);
+  }
+
+  do {
+    if (match_here(regexp, text)) {
+      return 1;
+    }
+  } while (*text++ != '\0');
+
+  return 0;
+}
+
+int match_here(char *regexp, char *text) {
+  if (regexp[0] == '\0') {
+    return 1;
+  }
+
+  if (regexp[1] == '*') {
+    return match_star(regexp[0], regexp+2, text);
+  }
+
+  if (regexp[0] == '$' && regexp[1] == '\0') {
+    return *text == '\0';
+  }
+
+   if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) {
+    return match_here(regexp+1, text+1);
+  }
+  
+  return 0;
+}
+
+int match_star(char c, char *regexp, char *text) {
+  do {
+    if (match_here(regexp, text)) {
+      return 1;
+    }
+  } while (*text!='\0' && (*text++ == c || c == '.'));
+  
+  return 0;
+}

regex/regex_driver.c

+#include <stdio.h>
+
+int match(char *regexp, char *text);
+
+int main(void) {
+  printf("%d\n", match("fuck", "fuck"));
+  printf("%d\n", match("fu.k", "fuck"));
+  printf("%d\n", match("fu.k", "fukk"));
+  printf("%d\n", match("^fu.k$", "fukk"));
+  printf("%d\n", match("^fu.k$", "fucka"));
+  printf("%d\n", match("^fu.k", "fucka"));
+
+  printf("Hello, World.\n");
+  return 1;
+}