|
| 1 | +/* |
| 2 | +* NIST Utils Class Library |
| 3 | +* clutils/Str.cc |
| 4 | +* April 1997 |
| 5 | +* K. C. Morris |
| 6 | +* David Sauder |
| 7 | +
|
| 8 | +* Development of this software was funded by the United States Government, |
| 9 | +* and is not subject to copyright. |
| 10 | +*/ |
| 11 | + |
| 12 | +#include "Str.h" |
| 13 | +#include <sstream> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +static int contiguous_block_number( unsigned char c ) |
| 17 | +{ |
| 18 | + if (c == 0) /* \0 */ |
| 19 | + return 0; |
| 20 | + if (c == 32) /* SPACE */ |
| 21 | + return 1; |
| 22 | + if (48 <= c && c <= 57) /* 0-9 */ |
| 23 | + return 2; |
| 24 | + if (97 <= c && c <= 122) /* a-z */ |
| 25 | + return 3; |
| 26 | + if (65 <= c && c <= 90) /* A-Z */ |
| 27 | + return 4; |
| 28 | + if (c == 95) /* _ */ |
| 29 | + return 5; |
| 30 | + if (33 <= c && c <= 34) /* !" */ |
| 31 | + return 6; |
| 32 | + if (c == 42) /* * */ |
| 33 | + return 7; |
| 34 | + if (36 <= c && c <= 38) /* $%& */ |
| 35 | + return 8; |
| 36 | + if (c == 46) /* . */ |
| 37 | + return 9; |
| 38 | + if (c == 35) /* # */ |
| 39 | + return 10; |
| 40 | + if (43 <= c && c <= 45) /* +,- */ |
| 41 | + return 11; |
| 42 | + if (40 <= c && c <= 41) /* () */ |
| 43 | + return 12; |
| 44 | + if (c == 63) /* ? */ |
| 45 | + return 13; |
| 46 | + if (c == 47) /* / */ |
| 47 | + return 14; |
| 48 | + if (58 <= c && c <= 62) /* :<=> */ |
| 49 | + return 15; |
| 50 | + if (c == 64) /* @ */ |
| 51 | + return 16; |
| 52 | + if (c == 91) /* [ */ |
| 53 | + return 17; |
| 54 | + if (c == 93) /* ] */ |
| 55 | + return 18; |
| 56 | + if (123 <= c && c <= 125) /* {|} */ |
| 57 | + return 19; |
| 58 | + if (c == 94) /* ^ */ |
| 59 | + return 20; |
| 60 | + if (c == 96) /* ` */ |
| 61 | + return 21; |
| 62 | + if (c == 126) /* ~ */ |
| 63 | + return 22; |
| 64 | + if (c == 92) /* \ */ |
| 65 | + return 23; |
| 66 | + if (c == 39) /* ' */ |
| 67 | + return 24; |
| 68 | + |
| 69 | + return 25; |
| 70 | +} |
| 71 | + |
| 72 | +static int entity_char_cmp( unsigned char c1, unsigned char c2 ) |
| 73 | +{ |
| 74 | + int block1 = contiguous_block_number( c1 ); |
| 75 | + int block2 = contiguous_block_number( c2 ); |
| 76 | + |
| 77 | + if (block1 != block2) |
| 78 | + return block1 - block2; |
| 79 | + |
| 80 | + return c1 - c2; |
| 81 | +} |
| 82 | + |
| 83 | +/**************************************************************//** |
| 84 | + ** \fn entity_name_cmp (const char * str1, const char * str2) |
| 85 | + ** \returns Comparison result |
| 86 | + ** Compares two strings according to P21 entity naming rules |
| 87 | + ** Returns < 0 when str1 less then str2 |
| 88 | + ** == 0 when str1 equals str2 |
| 89 | + ** > 0 when str1 greater then str2 |
| 90 | + ******************************************************************/ |
| 91 | +int entity_name_cmp( const char *str1, const char *str2 ) |
| 92 | +{ |
| 93 | + const unsigned char *s1 = ( const unsigned char * ) str1; |
| 94 | + const unsigned char *s2 = ( const unsigned char * ) str2; |
| 95 | + unsigned char c1, c2; |
| 96 | + |
| 97 | + do { |
| 98 | + c1 = ( unsigned char ) *s1++; |
| 99 | + c2 = ( unsigned char ) *s2++; |
| 100 | + if( c1 == '\0' ) |
| 101 | + return c1 - c2; /* 0 if both c1 and c2 are \0, otherwise returns -ve for c1 terminating first */ |
| 102 | + } while( c1 == c2 ); |
| 103 | + |
| 104 | + return entity_char_cmp( c1, c2 ); |
| 105 | +} |
0 commit comments