Kaynağa Gözat

ncd: NCDValue: add NCDValue_Compare

ambrop7 14 yıl önce
ebeveyn
işleme
edfc9e6eff
2 değiştirilmiş dosya ile 47 ekleme ve 0 silme
  1. 45 0
      ncd/NCDValue.c
  2. 2 0
      ncd/NCDValue.h

+ 45 - 0
ncd/NCDValue.c

@@ -328,3 +328,48 @@ NCDValue NCDValue_ListShift (NCDValue *o)
     
     return v;
 }
+
+int NCDValue_Compare (NCDValue *o, NCDValue *v)
+{
+    if (o->type == NCDVALUE_STRING && v->type == NCDVALUE_LIST) {
+        return -1;
+    }
+    
+    if (o->type == NCDVALUE_LIST && v->type == NCDVALUE_STRING) {
+        return 1;
+    }
+    
+    if (o->type == NCDVALUE_STRING && v->type == NCDVALUE_STRING) {
+        int cmp = strcmp(o->string, v->string);
+        if (cmp < 0) {
+            return -1;
+        }
+        if (cmp > 0) {
+            return 1;
+        }
+        return 0;
+    }
+    
+    NCDValue *x = NCDValue_ListFirst(o);
+    NCDValue *y = NCDValue_ListFirst(v);
+    
+    while (1) {
+        if (!x && y) {
+            return -1;
+        }
+        if (x && !y) {
+            return 1;
+        }
+        if (!x && !y) {
+            return 0;
+        }
+        
+        int res = NCDValue_Compare(x, y);
+        if (res) {
+            return res;
+        }
+        
+        x = NCDValue_ListNext(o, x);
+        y = NCDValue_ListNext(v, y);
+    }
+}

+ 2 - 0
ncd/NCDValue.h

@@ -65,4 +65,6 @@ int NCDValue_ListReadHead (NCDValue *o, int num, ...) WARN_UNUSED;
 NCDValue * NCDValue_ListGet (NCDValue *o, size_t pos);
 NCDValue NCDValue_ListShift (NCDValue *o);
 
+int NCDValue_Compare (NCDValue *o, NCDValue *v);
+
 #endif