[dovecot-cvs] dovecot-lda/src/libsieve comparator.c,1.1.1.1,1.2

cras at dovecot.org cras at dovecot.org
Sat Oct 15 16:12:28 EEST 2005


Update of /var/lib/cvs/dovecot-lda/src/libsieve
In directory talvi:/tmp/cvs-serv25380

Modified Files:
	comparator.c 
Log Message:
Updated ascii-numeric comparator from Cyrus CVS



Index: comparator.c
===================================================================
RCS file: /var/lib/cvs/dovecot-lda/src/libsieve/comparator.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- comparator.c	29 Mar 2005 18:37:59 -0000	1.1.1.1
+++ comparator.c	15 Oct 2005 13:12:26 -0000	1.2
@@ -266,16 +266,78 @@
  *  number   A ? B    B > A 
  *  not-num  A > B    A == B
  */
+
+ /* From RFC 2244:
+  *
+  * The i;ascii-numeric comparator interprets strings as decimal
+  * positive integers represented as US-ASCII digits.  All values
+  * which do not begin with a US-ASCII digit are considered equal
+  * with an ordinal value higher than all non-NIL single-valued
+  * attributes.  Otherwise, all US-ASCII digits (octet values
+  * 0x30 to 0x39) are interpreted starting from the beginning of
+  * the string to the first non-digit or the end of the string.
+  */
+
 static int ascii_numeric_cmp(const char *text, const char *pat)
 {
+    unsigned text_digit_len;
+    unsigned pat_digit_len;
+
     if (isdigit((int) *pat)) {
 	if (isdigit((int) *text)) {
-	    return (atoi(text) - atoi(pat));
+	    /* Count how many digits each string has */
+	    for (text_digit_len = 0;
+		 isdigit((int) text[text_digit_len]);
+		 text_digit_len++);
+	    for (pat_digit_len = 0;
+		 isdigit((int) pat[pat_digit_len]);
+		 pat_digit_len++);
+
+	    if (text_digit_len < pat_digit_len) {
+		/* Pad "text" with leading 0s */
+		while (pat_digit_len > text_digit_len) {
+		    /* "text" can only be less or equal to "pat" */
+		    if ('0' < *pat) {
+			return (-1); 
+		    }
+		    pat++;
+		    pat_digit_len--;
+		}
+	    } else if (text_digit_len > pat_digit_len) {
+		/* Pad "pad" with leading 0s */
+		while (text_digit_len > pat_digit_len) {
+		    /* "pad" can only be greater or equal to "text" */
+		    if (*text > '0') {
+			return 1;
+		    }
+		    text++;
+		    text_digit_len--;
+		}
+	    }
+
+	    /* CLAIM: If we here, we have two non-empty digital suffixes
+	       of equal length */
+	    while (text_digit_len > 0) {
+		if (*text < *pat) {
+			return -1;
+		} else if (*text > *pat) {
+			return 1;
+		}
+		/* Characters are equal, carry on */
+		text++;
+		pat++;
+		text_digit_len--;
+	    }
+
+	    return (0);
 	} else {
 	    return 1;
 	}
-    } else if (isdigit((int) *text)) return -1;
-    else return 0; /* both not digits */
+    } else if (isdigit((int) *text)) {
+	return -1;
+    } else {
+	return 0; /* both not digits */
+    }
 }
 
 static comparator_t *lookup_rel(int relation)



More information about the dovecot-cvs mailing list