changeset 1585:cff20c82a797 draft

expr.c: Added '==' sign into list of operations. Also added support for regex pattern match.
author Ashwini Sharma <ak.ashwini1981@gmail.com>
date Sat, 29 Nov 2014 18:38:05 -0600
parents 1250bc526316
children 6db5787d340b
files toys/pending/expr.c
diffstat 1 files changed, 18 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/expr.c	Fri Nov 28 17:30:46 2014 -0600
+++ b/toys/pending/expr.c	Sat Nov 29 18:38:05 2014 -0600
@@ -111,7 +111,23 @@
 
 static void re(struct value *lhs, const struct value *rhs)
 {
-  error_exit("regular expression match not implemented");
+  regex_t rp;
+  regmatch_t rm[2];
+
+  xregcomp(&rp, rhs->s, 0);
+  if (!regexec(&rp, lhs->s, 2, rm, 0) && rm[0].rm_so == 0) {
+    if (rp.re_nsub > 0 && rm[1].rm_so >= 0) 
+      lhs->s = xmprintf("%.*s", rm[1].rm_eo - rm[1].rm_so, lhs->s+rm[1].rm_so);
+    else {
+      lhs->i = rm[0].rm_eo;
+      lhs->s = 0;
+    }
+  } else {
+    if (!rp.re_nsub) {
+      lhs->i = 0;
+      lhs->s = 0;
+    } else lhs->s = "";
+  }
 }
 
 static void mod(struct value *lhs, const struct value *rhs)
@@ -203,6 +219,7 @@
   {"|",   or  },
   {"&",   and },
   {"=",   eq  },
+  {"==",  eq  },
   {">",   gt  },
   {">=",  gte },
   {"<",   lt  },