/* Modified for MariaDB by Roberto Spadim From: http://sourcefrog.net/projects/natsort/ strnatcmp.c -- Perform 'natural order' comparisons of strings in C. Copyright (C) 2000 by Martin Pool This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ /* TODO: support charsets / collations create a function that return a canonical string from another string */ #ifdef STANDARD /* STANDARD is defined, don't use any mysql functions */ #include #include #include #include #ifdef __WIN__ typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */ typedef __int64 longlong; #else typedef unsigned long long ulonglong; typedef long long longlong; #endif /*__WIN__*/ #else #include #if defined(MYSQL_SERVER) #include /* To get strmov() */ #else /* when compiled as standalone */ #include #include #define strmov(a,b) stpcpy(a,b) #define bzero(a,b) memset(a,0,b) #endif #endif #include #include /* #if defined(__GNUC__) # define UNUSED __attribute__((__unused__)) #else # define UNUSED #endif */ my_bool strnatcmp_init(UDF_INIT* initid, UDF_ARGS* args, char* message) { if (args->arg_count != 2) { strcpy(message,"Wrong arguments to strnatcmp; must be (str,str)"); return 1; } if ( args->arg_type[0] != STRING_RESULT ) { args->arg_type[0] = STRING_RESULT; } if ( args->arg_type[1] != STRING_RESULT ) { args->arg_type[1] = STRING_RESULT; } return 0; } void strnatcmp_deinit(UDF_INIT* initid) { if (initid->ptr) free(initid->ptr); } longlong strnatcmp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { return (longlong) strnatcmp_ex( args->args[0], strlen(args->args[0]), args->args[1], strlen(args->args[1]), 0); } my_bool strnatcasecmp_init(UDF_INIT* initid, UDF_ARGS* args, char* message) { if (args->arg_count != 2) { strcpy(message,"Wrong arguments to strnatcasecmp; must be (str,str)"); return 1; } if ( args->arg_type[0] != STRING_RESULT ) { args->arg_type[0] = STRING_RESULT; } if ( args->arg_type[1] != STRING_RESULT ) { args->arg_type[1] = STRING_RESULT; } return 0; } void strnatcasecmp_deinit(UDF_INIT* initid) { if (initid->ptr) free(initid->ptr); } longlong strnatcasecmp(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) { return (longlong) strnatcmp_ex( args->args[0], strlen(args->args[0]), args->args[1], strlen(args->args[1]), 1); } static int compare_right(char const **a, char const *aend, char const **b, char const *bend) { int bias = 0; /* The longest run of digits wins. That aside, the greatest value wins, but we can't know that it will until we've scanned both numbers to know that they have the same magnitude, so we remember it in BIAS. */ for(;; (*a)++, (*b)++) { if ((*a == aend || !isdigit((int)(unsigned char)**a)) && (*b == bend || !isdigit((int)(unsigned char)**b))) return bias; else if (*a == aend || !isdigit((int)(unsigned char)**a)) return -1; else if (*b == bend || !isdigit((int)(unsigned char)**b)) return +1; else if (**a < **b) { if (!bias) bias = -1; } else if (**a > **b) { if (!bias) bias = +1; } } return 0; } static int compare_left(char const **a, char const *aend, char const **b, char const *bend) { /* Compare two left-aligned numbers: the first to have a different value wins. */ for(;; (*a)++, (*b)++) { if ((*a == aend || !isdigit((int)(unsigned char)**a)) && (*b == bend || !isdigit((int)(unsigned char)**b))) return 0; else if (*a == aend || !isdigit((int)(unsigned char)**a)) return -1; else if (*b == bend || !isdigit((int)(unsigned char)**b)) return +1; else if (**a < **b) return -1; else if (**a > **b) return +1; } return 0; } int strnatcmp_ex(char const *a, size_t a_len, char const *b, size_t b_len, int fold_case) { unsigned char ca, cb; char const *ap, *bp; char const *aend = a + a_len, *bend = b + b_len; int fractional, result; short leading = 1; if (a_len == 0 || b_len == 0) return a_len - b_len; ap = a; bp = b; while (1) { ca = *ap; cb = *bp; /* skip over leading zeros */ while (leading && ca == '0' && (ap+1 < aend) && isdigit(*(ap+1))) { ca = *++ap; } while (leading && cb == '0' && (bp+1 < bend) && isdigit(*(bp+1))) { cb = *++bp; } leading = 0; /* Skip consecutive whitespace */ while (isspace((int)(unsigned char)ca)) { ca = *++ap; } while (isspace((int)(unsigned char)cb)) { cb = *++bp; } /* process run of digits */ if (isdigit((int)(unsigned char)ca) && isdigit((int)(unsigned char)cb)) { fractional = (ca == '0' || cb == '0'); if (fractional) result = compare_left(&ap, aend, &bp, bend); else result = compare_right(&ap, aend, &bp, bend); if (result != 0) return result; else if (ap == aend && bp == bend) /* End of the strings. Let caller sort them out. */ return 0; else { /* Keep on comparing from the current point. */ ca = *ap; cb = *bp; } } if (fold_case) { ca = toupper((int)(unsigned char)ca); cb = toupper((int)(unsigned char)cb); } if (ca < cb) return -1; else if (ca > cb) return +1; ++ap; ++bp; if (ap >= aend && bp >= bend) /* The strings compare the same. Perhaps the caller will want to call strcmp to break the tie. */ return 0; else if (ap >= aend) return -1; else if (bp >= bend) return 1; } } #define NATURAL_MAX_SIZE 65535 #define NATURAL_NUMBER_SIZE 20 my_bool natstring_init(UDF_INIT* initid, UDF_ARGS* args, char* message) { if (args->arg_count != 1) { strcpy(message,"Wrong arguments to natstring; must be (str)"); return 1; } if ( args->arg_type[0] != STRING_RESULT ) { args->arg_type[0] = STRING_RESULT; } initid->max_length=NATURAL_MAX_SIZE; return 0; } void natstring_deinit(UDF_INIT* initid) { if (initid->ptr) free(initid->ptr); } char *natstring(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { /* example: "hello 1 person in 2 tables" return: "hello 0000000000001 person in 0000000000002 tables" canonical problem: how to rewrite numbers? 1) 1 vs 01 -> the first idea is use bigint precision 20 digits => "18446744073709551615"/"-9223372036854775807" "1" and "01" = "00000000000000000001" 2) decimal: 0.1 using the same idea of (1): 0.1 = "0"."1" => "00000000000000000000.00000000000000000001" or maybe understand that's a fraction? 0.1 = "00000000000000000000.10000000000000000000" 3) 10e-1 vs 0.1 that's a problem since 10e-1 and 0.1 is the same value, but when you read 10e-1 you don't think about 0.1, i'm right? maybe we can use (2) here -> rewrite to 00000000000000000010e-00000000000000000001 00000000000000000010e-00000000000000000001 vs 00000000000000000000.00000000000000000001 strnatcasecmp('10e-1','0.1') => return "1" strnatcmp('10e-1','0.1') => return "1" 4) what about numbers with more than 20 digits? example: PI = "3.1415926535897932384626433832795028841971693993751" ? rewrite with length = ceil(size/20)*20 ? or maybe a leading zeros "0000000000000000000000000000000000000000000000000000000001" could be rewrite to "1" => "00000000000000000001" 5) special numbers... example: '1.1.1' should be understood as: integer.integer.integer instead of integer.fraction.integer */ /* TODO: implement fractions */ char lead_zero,fraction,last_char; long i,l,w,pos,start_number,w_end; if (!args->args[0]) /* Null argument */ { *is_null=1; return 0; } w_end=args->lengths[0]; if(w_end==0) { if(!(result = (char*) malloc( sizeof(char) ))){ *is_null=1; *error=1; return 0; } *length=0; result[0]=0; return result; } if(!(result = (char*) malloc( sizeof(char)*NATURAL_MAX_SIZE ))){ *is_null=1; *error=1; return 0; } lead_zero=0; /* remove left zeros */ fraction=0; /* understand number as fraction */ last_char=0; /* remove spaces */ pos=0; /* current position in result buffer */ start_number=-1; /* position of first number */ for (i=0;i < w_end; i++) { /* number (0-9) */ if ( isdigit( args->args[0][i] )) { last_char = args->args[0][i]; if (args->args[0][i]=='0' && start_number==-1){ /* lead zero */ lead_zero=1; continue; } if (start_number==-1) start_number=i; /* number start here */ continue; } /* 0 number */ if(lead_zero==1 && start_number==-1){ /* zero */ for(w=0;w=NATURAL_MAX_SIZE) goto fim; } } lead_zero=0; /* write the number */ if (start_number!=-1) { /* end of a number */ if((i-start_number)>NATURAL_NUMBER_SIZE) { /* too big number, just copy */ for(l=start_number;l<=i;l++) { result[pos]=args->args[0][l]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; } }else{ if(fraction==1) { /* copy number */ for (l=start_number;largs[0][l]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; } /* put zeros at end */ for(w=0;w=NATURAL_MAX_SIZE) goto fim; } }else{ /* put zeros at start */ for(w=0;w=NATURAL_MAX_SIZE) goto fim; } /* copy number */ for (l=start_number;largs[0][l]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; } } } start_number=-1; } /* character */ if (start_number==-1) { /* a character... */ if(last_char==' ' && args->args[0][i]==' ') continue; /* just copy */ last_char = args->args[0][i]; result[pos] = args->args[0][i]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; continue; } last_char = args->args[0][i]; } /* here we must check if the last chars were a number */ if(lead_zero==1 && start_number==-1){ /* last number was a zero */ for(w=0;w=NATURAL_MAX_SIZE) goto fim; } } lead_zero=0; if(start_number==-1) goto fim; /* last number */ if((i-start_number)>NATURAL_NUMBER_SIZE) { /* too big number, just copy */ for(l=start_number;l<=i;l++) { result[pos]=args->args[0][l]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; } }else{ if(fraction==1) { /* copy number */ for (l=start_number;largs[0][l]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; } /* put zeros at end */ for(w=0;w=NATURAL_MAX_SIZE) goto fim; } }else{ /* put zeros at start */ for(w=0;w=NATURAL_MAX_SIZE) goto fim; } /* copy number */ for (l=start_number;largs[0][l]; pos++; if(pos+2>=NATURAL_MAX_SIZE) goto fim; } } } fim: /* result[pos+1]=0; */ *length=pos; return result; }