str.c

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <limits.h>
00003 #include <ctype.h>
00004 
00005 #include "util.h"
00006 #include "str.h"
00007 #include "valor.h"
00008 
00009 /**
00010  * \file str.c Operações com strings. 
00011  */
00012 
00013 //-----------------------------------------------------------------------------
00014 /// Compara 2 strings.
00015 //-----------------------------------------------------------------------------
00016 int str_compare(const void *v1, const void *v2) {
00017         char *c1 = (char*)v1, *c2 = (char*)v2;
00018         return strcmp(c1, c2);
00019 }
00020 
00021 //-----------------------------------------------------------------------------
00022 /// Copia um string.
00023 //-----------------------------------------------------------------------------
00024 char* str_clone(const char* str) {
00025         return strdup(str);
00026 }
00027 
00028 /// Define qual implementação da função str_hash_code() será usada.
00029 ///     \sa str_hash_code()
00030 #define STR_HASH_CODE1
00031 
00032 //-----------------------------------------------------------------------------
00033 /** Retorna um hashcode para a chave key. Possui 3 implementações:
00034 STR_HASH_CODE1: utiliza um vetor valor[256][32] de números primos. Para cada caracter key[i] da chave soma valor[ (int)key[i] ][i] ao hashcode.
00035 STR_HASH_CODE2: implementação abandonada (igual a STR_HASH_CODE1, mas limitava a fazer hashcode dos 32 primeiros caracteres do string).
00036 STR_HASH_CODE3: implementação original (numero de colisões (43386 para os 110124 termos de wsj88) ligeiramente superior aos 2 de cima (43319)).
00037         \sa valor.h
00038 */
00039 //-----------------------------------------------------------------------------
00040 int str_hash_code(char *key) {
00041         int tam = strlen(key);
00042         int i;
00043         int soma = 0;
00044 
00045 #ifdef STR_HASH_CODE1
00046         if (tam > 32) {
00047                 int j=0;
00048                 for (i=0; i<tam; i++) {
00049                         soma += valor[ (int)key[i] ][j];
00050                         j++;
00051                         if (j>=32) j=0;
00052                 }
00053         } else {
00054                 for (i=0; i<tam; i++) {
00055                         soma += valor[ (int)key[i] ][i];
00056                 }
00057         }
00058 #endif
00059 #ifdef STR_HASH_CODE2
00060         if (tam > 32) tam = 32;
00061         for (i=0; i<tam; i++) {
00062                 soma += valor[ (int)key[i] ][i];
00063         }
00064 #endif
00065 #ifdef STR_HASH_CODE3
00066         int off = 0;
00067         
00068         if (tam > 0) {
00069                 do {
00070                         soma = 31*soma + (int)key[off++];
00071                 } while (--tam > 0);
00072         }
00073 #endif
00074         if(soma < 0) soma = (-1) * soma;
00075         return soma;
00076 }
00077 
00078 //-----------------------------------------------------------------------------
00079 /// Destrói um string.
00080 //-----------------------------------------------------------------------------
00081 void str_destroy(void *value) {
00082         char *str = (char*)value;
00083         free(str);
00084 }
00085 
00086 //-----------------------------------------------------------------------------
00087 /// Elimina espaços, \r, quebra de linha e \t do início e do final de um string.
00088 //-----------------------------------------------------------------------------
00089 void str_trim(char *str) {
00090         int i, j, len;
00091 
00092         len = strlen(str);
00093         i = 0;
00094         while((i < len) && ((str[i] == ' ') || (str[i] == '\t') || 
00095                         (str[i] == '\n') || (str[i] == '\r'))) {
00096                 i++;
00097         }
00098         j = len - 1;
00099         while((j >= 0) && ((str[j] == ' ') || (str[j] == '\t') ||
00100                         (str[i] == '\n') || (str[i] == '\r'))) {
00101                 j--;
00102         }
00103 
00104         if (j >= 0) str[j + 1] = '\0';
00105         
00106         if (i <= j) {
00107                 strcpy(str, str + i);
00108         } else {
00109                 strcpy(str, "");
00110         }
00111 }
00112 
00113 //-----------------------------------------------------------------------------
00114 /// Passa um string para minúsculas.
00115 //-----------------------------------------------------------------------------
00116 void str_to_lower(char *str) {
00117         int i, len = strlen(str);
00118         for(i=0; i<len; i++) {
00119                 str[i] = (char)tolower((int)str[i]);
00120         }
00121 }
00122 
00123 //-----------------------------------------------------------------------------
00124 /// Passa um string para maiúsculas.
00125 //-----------------------------------------------------------------------------
00126 void str_to_upper(char *str) {
00127         int i, len = strlen(str);
00128         for(i=0; i<len; i++) {
00129                 str[i] = (char)toupper((int)str[i]);
00130         }
00131 }
00132 
00133 //-----------------------------------------------------------------------------
00134 /// Elimina tags de um string.
00135 //-----------------------------------------------------------------------------
00136 void str_remove_tags(char *str) {
00137         int begin = 0, end = 0, i, j;
00138         int opened = FALSE;
00139         int len = strlen(str);
00140         
00141         for (i=0; i<len; i++) {
00142                 if (str[i] == '<') {
00143                         begin = i;
00144                         opened = TRUE;
00145                 } else if (opened && str[i] == '>') {
00146                         end = i;
00147                         opened = FALSE;
00148                         for (j=begin; j<=end; j++) {
00149                                 str[j] = ' ';
00150                         }
00151                 }
00152         }
00153         str_trim(str);
00154 }

Generated on Tue Jan 17 19:18:39 2006 for Void by  doxygen 1.4.6