├── reg ├── ascii.c └── reg.c └── uniq ├── Makefile ├── fbUniq.c ├── hashtable.c └── hashtable.h /reg/ascii.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define BUFFSIZE 4096 8 | 9 | int 10 | main(int argc, char *argv[]) 11 | { 12 | int i; 13 | char buf[BUFFSIZE] = { 0 }; 14 | 15 | while (fgets(buf, BUFFSIZE, stdin)) { 16 | i = 0; 17 | while(buf[i]) { 18 | if ((!isprint(buf[i])) && (buf[i] != '\n') && (buf[i] != '\r') ) { /* 不可打印字符 */ 19 | goto clear; 20 | } 21 | i++; 22 | } 23 | if(buf[0]) { 24 | printf("%s",buf); 25 | } 26 | clear: 27 | memset(buf, 0, BUFFSIZE); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /reg/reg.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define BUFFSIZE 4096 8 | 9 | int trans_endian(int big) { 10 | return ((big & 0xff) << 16) 11 | | (big & 0xff00) 12 | | ((big & 0xff0000) >> 16); 13 | } 14 | 15 | int utf8_to_unicode(int utf8) { 16 | int high = (utf8 & 0xf0000) >> 16; 17 | int middle = (utf8 & 0x3f00) >> 8; 18 | int low = utf8 & 0x3f; 19 | return (high << 12) | (middle << 6) | low; 20 | } 21 | 22 | int is_chinese(int ch) { 23 | return (ch > 0x4E00 && ch < 0x9FCB)? 1: 0; 24 | } 25 | 26 | int 27 | main(int argc, char *argv[]) 28 | { 29 | int i; 30 | char buf[BUFFSIZE] = { 0 }; 31 | 32 | int utf8; 33 | while (fgets(buf, BUFFSIZE, stdin)) { 34 | i = 0; 35 | while(buf[i]) { 36 | if ((buf[i] & 0x80) == 0) { /* ascii字符 */ 37 | if ((!isprint(buf[i])) && (buf[i] != '\n') && (buf[i] != '\r') ) { /* 不可打印字符 */ 38 | goto clear; 39 | } 40 | i++; 41 | } else { 42 | ((char *)&utf8)[0] = buf[i++]; 43 | ((char *)&utf8)[1] = buf[i++]; 44 | ((char *)&utf8)[2] = buf[i++]; 45 | if (!is_chinese(utf8_to_unicode(trans_endian(utf8)))) { /*不是中文*/ 46 | goto clear; 47 | } 48 | } 49 | } 50 | if(buf[0]) { 51 | printf("%s",buf); 52 | } 53 | clear: 54 | memset(buf, 0, BUFFSIZE); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /uniq/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc hashtable.c fbUniq.c -O3 -o fbUniq 3 | install: 4 | mv fbUniq /usr/bin/ 5 | clear: 6 | rm -rf /usr/bin/fbUniq 7 | 8 | -------------------------------------------------------------------------------- /uniq/fbUniq.c: -------------------------------------------------------------------------------- 1 | #include "hashtable.h" 2 | 3 | #define BUFFSIZE 409600 4 | 5 | int 6 | main (void) 7 | { 8 | 9 | HashTable *hashtable = malloc (sizeof (HashTable)); 10 | hash_table_init(hashtable); 11 | char buf[BUFFSIZE] = { 0 }; 12 | 13 | while (fgets(buf, BUFFSIZE, stdin)) { 14 | bool ret = hash_table_insert_str(hashtable, buf); 15 | if (ret) { 16 | printf("%s", buf); 17 | } 18 | } 19 | 20 | //hash_table_release(hashtable); 21 | free (hashtable); 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /uniq/hashtable.c: -------------------------------------------------------------------------------- 1 | #include "hashtable.h" 2 | 3 | /* initialize hash table */ 4 | void 5 | hash_table_init (HashTable *hashtable) 6 | { 7 | hashtable->hash_table_max_size = HASH_TABLE_MAX_SIZE; 8 | hashtable->hash_size = 0; 9 | hashtable->hashnode = (HashNode **)calloc (sizeof(HashNode*), \ 10 | HASH_TABLE_MAX_SIZE); 11 | } 12 | 13 | HashNode* 14 | create_new_node (const char *skey) { 15 | HashNode *pNewNode; 16 | 17 | pNewNode = (HashNode*) malloc (sizeof (HashNode) + strlen(skey)) ; 18 | strcpy (pNewNode->sKey, skey); 19 | return pNewNode; 20 | } 21 | 22 | 23 | inline unsigned long 24 | hash_func (const char *arKey) 25 | { 26 | unsigned int nKeyLength = strlen(arKey); 27 | register unsigned long hash = 5381; 28 | 29 | /* variant with the hash unrolled eight times */ 30 | for (; nKeyLength >= 8; nKeyLength -= 8) { 31 | hash = ((hash << 5) + hash) + *arKey++; 32 | hash = ((hash << 5) + hash) + *arKey++; 33 | hash = ((hash << 5) + hash) + *arKey++; 34 | hash = ((hash << 5) + hash) + *arKey++; 35 | hash = ((hash << 5) + hash) + *arKey++; 36 | hash = ((hash << 5) + hash) + *arKey++; 37 | hash = ((hash << 5) + hash) + *arKey++; 38 | hash = ((hash << 5) + hash) + *arKey++; 39 | } 40 | switch (nKeyLength) { 41 | case 7: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 42 | case 6: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 43 | case 5: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 44 | case 4: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 45 | case 3: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 46 | case 2: hash = ((hash << 5) + hash) + *arKey++; /* fallthrough... */ 47 | case 1: hash = ((hash << 5) + hash) + *arKey++; break; 48 | case 0: break; 49 | } 50 | return hash; 51 | } 52 | 53 | 54 | /* string hash function */ 55 | /* 56 | unsigned int 57 | hash_table_hash_str (const char* skey) 58 | { 59 | const char *p; 60 | unsigned int hash; 61 | 62 | p = (const char*)skey; 63 | hash = *p; 64 | if (hash) { 65 | for (p += 1; *p != '\0'; p++) 66 | hash = (hash << 5) - hash + *p; 67 | } 68 | return hash; 69 | } 70 | */ 71 | 72 | /*insert key-value into hash table, if key is exist, 73 | *it will overwrite old value, use link list to slove 74 | *hash conflict,*/ 75 | bool 76 | hash_table_insert_str (HashTable *hashtable, const char* skey) 77 | { 78 | size_t pos; 79 | HashNode *pHead; 80 | HashNode *pLast; 81 | HashNode *pNewNode; 82 | 83 | pos = hash_pos (skey); 84 | pHead = (hashtable->hashnode)[pos]; 85 | 86 | if (pHead) { 87 | do { 88 | if (strcmp (pHead->sKey, skey) == 0) { 89 | return false; 90 | } 91 | pLast = pHead; 92 | } while (pHead = pHead->pNext); 93 | pNewNode = create_new_node(skey); 94 | pLast->pNext = pNewNode; 95 | } else { 96 | pNewNode = create_new_node(skey); 97 | (hashtable->hashnode)[pos] = pNewNode; 98 | } 99 | return true; 100 | } 101 | 102 | /*free the memory of the hash table*/ 103 | void 104 | hash_table_release (HashTable *hashtable) 105 | { 106 | size_t i; 107 | HashNode *pHead, *pTemp; 108 | 109 | for (i = 0; i < hashtable->hash_table_max_size; i++) { 110 | if ((pHead = (hashtable->hashnode)[i])) { 111 | while (pHead) { 112 | pTemp = pHead; 113 | pHead = pHead->pNext; 114 | free (pTemp->sKey); 115 | free (pTemp); 116 | } 117 | } 118 | } 119 | free (hashtable->hashnode); 120 | } 121 | -------------------------------------------------------------------------------- /uniq/hashtable.h: -------------------------------------------------------------------------------- 1 | #ifndef HASHTABLE_H 2 | #define HASHTABLE_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifndef bool 10 | # define bool unsigned char 11 | #endif 12 | 13 | #ifndef false 14 | # define false (0) 15 | #endif 16 | 17 | #ifndef true 18 | # define true (!(false)) 19 | #endif 20 | 21 | #define HASH_TABLE_MAX_SIZE (1 << 27) 22 | 23 | #define hash_pos(skey) \ 24 | hash_func((skey)) % HASH_TABLE_MAX_SIZE 25 | 26 | #define hashTable_size(hashtable) \ 27 | ((hashtable)->hash_size) 28 | 29 | #define hashTable_max_size(hashtable) \ 30 | (hashtable)->hash_table_max_size 31 | 32 | 33 | #define mallocStr(str) \ 34 | (char*) calloc (sizeof(char), strlen (str) + 1) 35 | 36 | 37 | 38 | typedef struct hashnode HashNode; 39 | struct hashnode { 40 | HashNode *pNext; 41 | char sKey[1]; 42 | } __attribute__((packed)); 43 | 44 | typedef struct hashtable { 45 | HashNode **hashnode; 46 | size_t hash_table_max_size; 47 | size_t hash_size; 48 | } HashTable; 49 | 50 | void hash_table_init (HashTable *hashtable); 51 | 52 | unsigned int hash_table_hash_str (const char* skey); 53 | 54 | bool hash_table_insert_str (HashTable *hashtable, 55 | const char* skey); 56 | 57 | void hash_table_release (HashTable *hashtable); 58 | 59 | #endif /* !HASHTABLE_H */ 60 | --------------------------------------------------------------------------------