00001 00002 // 00003 // Copyright (C) 2000 00004 // Ralf Westram 00005 // (Coded@ReallySoft.de) 00006 // 00007 // Permission to use, copy, modify, distribute and sell this software 00008 // and its documentation for any purpose is hereby granted without fee, 00009 // provided that the above copyright notice appear in all copies and 00010 // that both that copyright notice and this permission notice appear 00011 // in supporting documentation. Ralf Westram makes no 00012 // representations about the suitability of this software for any 00013 // purpose. It is provided "as is" without express or implied warranty. 00014 // 00015 // This code is part of my library. 00016 // You may find a more recent version at http://www.reallysoft.de/ 00017 // 00019 00020 #ifndef TOOLS_H 00021 #define TOOLS_H 00022 00023 #ifndef RSDEF_H 00024 #include "rsdef.h" 00025 #endif 00026 #ifndef GENERIC_ALGO_H 00027 #include "generic_algo.h" 00028 #endif 00029 00030 00031 namespace rs { 00032 00034 namespace internal { 00035 inline int diff(size_t v1, size_t v2) { 00036 return v1<v2 ? -int(v2-v1) : int(v1-v2); 00037 } 00038 } 00040 00042 namespace str { 00043 00049 std::string vstrf(const char *format, va_list argPtr); 00050 00056 std::string strf(const char *format, ...); 00057 00063 inline std::string cropString(const std::string& s) { 00064 size_t start = s.find_first_not_of(' '); 00065 size_t end = s.find_last_not_of(' '); 00066 return (start==std::string::npos || end==std::string::npos) 00067 ? "" 00068 : std::string(s, start, end-start+1); 00069 } 00070 00076 inline char *strdup(const std::string& s) { 00077 char *buf = (char*)std::malloc(s.length()+1); 00078 memcpy(buf, s.data(), s.length()); 00079 buf[s.length()] = 0; 00080 return buf; 00081 } 00082 00083 // ---------------------------------------- strncmp 00084 00101 inline int strncmp(const std::string& s1, const char *s2, size_t maxcmp) { 00102 size_t l1 = s1.length(); 00103 if (l1<maxcmp) { 00104 int cmp = std::strncmp(s1.data(), s2, l1); 00105 return cmp ? cmp : (s2[l1]==0 ? 0 : -1); 00106 } 00107 return std::strncmp(s1.data(), s2, maxcmp); 00108 } 00113 inline int strncmp(const char* s1, const std::string& s2, size_t maxcmp) { 00114 return -strncmp(s2, s1, maxcmp); 00115 } 00116 00121 inline int strncmp(const std::string& s1, const std::string& s2, size_t maxcmp) { 00122 size_t Len1 = s1.length(); 00123 size_t Len2 = s2.length(); 00124 00125 if (Len1<maxcmp || Len2<maxcmp) { // one string is shorter than maxcmp 00126 int cmp = std::strncmp(s1.data(), s2.data(), gen_algo::min(Len1, Len2)); 00127 return cmp ? cmp : internal::diff(Len1, Len2); 00128 } 00129 return std::strncmp(s1.data(), s2.data(), maxcmp); 00130 } 00131 00133 00134 // ---------------------------------------- strnicmp 00135 00152 inline int strnicmp(const char *s1, const char *s2, size_t maxcmp) { 00153 int cmp = 0; 00154 size_t idx = 0; 00155 while (!cmp && maxcmp--) { 00156 if (!s1[idx]) return s2[idx] ? -1 : 0; 00157 if (!s2[idx]) return 1; 00158 cmp = tolower(s1[idx]) - tolower(s2[idx]); 00159 ++idx; 00160 } 00161 return cmp; 00162 } 00163 00168 inline int strnicmp(const std::string& s1, const char *s2, size_t maxcmp) { 00169 size_t l1 = s1.length(); 00170 if (l1<maxcmp) { 00171 int cmp = strnicmp(s1.data(), s2, l1); 00172 return cmp ? cmp : (s2[l1]==0 ? 0 : -1); 00173 } 00174 return strnicmp(s1.data(), s2, maxcmp); 00175 } 00180 inline int strnicmp(const char* s1, const std::string& s2, size_t maxcmp) { 00181 return -strnicmp(s2, s1, maxcmp); 00182 } 00183 00188 inline int strnicmp(const std::string& s1, const std::string& s2, size_t maxcmp) { 00189 size_t Len1 = s1.length(); 00190 size_t Len2 = s2.length(); 00191 00192 if (Len1<maxcmp || Len2<maxcmp) { // one string is shorter than maxcmp 00193 int cmp = strnicmp(s1.data(), s2.data(), gen_algo::min(Len1, Len2)); 00194 return cmp ? cmp : internal::diff(Len1, Len2); 00195 } 00196 return strnicmp(s1.data(), s2.data(), maxcmp); 00197 } 00198 00200 00201 // ---------------------------------------- stricmp 00202 00218 inline int stricmp(const char *s1, const char *s2) { 00219 int cmp = 0; 00220 size_t idx = 0; 00221 while (!cmp) { 00222 if (!s1[idx]) return s2[idx] ? -1 : 0; 00223 if (!s2[idx]) return 1; 00224 cmp = tolower(s1[idx]) - tolower(s2[idx]); 00225 ++idx; 00226 } 00227 return cmp; 00228 } 00229 00234 inline int stricmp(const std::string& s1, const char *s2) { 00235 int cmp = 0; 00236 size_t idx = 0; 00237 size_t Len1 = s1.length(); 00238 std::string::const_iterator iter1 = s1.begin(); 00239 while (!cmp) { 00240 if (Len1==idx) return s2[idx] ? -1 : 0; 00241 if (!s2[idx]) return 1; 00242 cmp = tolower(*iter1) - tolower(s2[idx]); 00243 ++idx; 00244 ++iter1; 00245 } 00246 return cmp; 00247 } 00252 inline int stricmp(const char *s1, const std::string& s2) { 00253 return -stricmp(s2, s1); 00254 } 00259 inline int stricmp(const std::string& s1, const std::string& s2) { 00260 size_t Len1 = s1.length(); 00261 size_t Len2 = s2.length(); 00262 int cmp = strnicmp(s1, s2, gen_algo::min(Len1, Len2)); 00263 return cmp ? cmp : internal::diff(Len1, Len2); 00264 } 00265 00267 00268 // ---------------------------------------- strscmp 00269 00288 inline int strscmp(const char *s1, const char *s2) { 00289 int cmp = 0; 00290 size_t idx = 0; 00291 while (!cmp) { 00292 if (!s1[idx] || !s2[idx]) break; 00293 cmp = s1[idx]-s2[idx]; 00294 ++idx; 00295 } 00296 return cmp; 00297 } 00298 00303 inline int strscmp(const std::string& s1, const char *s2) { 00304 int cmp = 0; 00305 size_t idx = 0; 00306 size_t Len1 = s1.length(); 00307 std::string::const_iterator iter1 = s1.begin(); 00308 while (!cmp) { 00309 if (idx==Len1 || !s2[idx]) break; 00310 cmp = *iter1-s2[idx]; 00311 ++idx; 00312 ++iter1; 00313 } 00314 return cmp; 00315 } 00320 inline int strscmp(const char *s1, const std::string& s2) { 00321 return -strscmp(s2, s1); 00322 } 00323 00328 inline int strscmp(const std::string& s1, const std::string& s2) { 00329 return strncmp(s1, s2, gen_algo::min(s1.length(), s2.length())); 00330 } 00331 00333 00334 // ---------------------------------------- strsicmp 00335 00354 inline int strsicmp(const char *s1, const char *s2) { 00355 int cmp = 0; 00356 size_t idx = 0; 00357 while (!cmp) { 00358 if (!s1[idx] || !s2[idx]) break; 00359 cmp = tolower(s1[idx])-tolower(s2[idx]); 00360 ++idx; 00361 } 00362 return cmp; 00363 } 00364 00369 inline int strsicmp(const std::string& s1, const char *s2) { 00370 int cmp = 0; 00371 size_t idx = 0; 00372 size_t Len1 = s1.length(); 00373 std::string::const_iterator iter1 = s1.begin(); 00374 while (!cmp) { 00375 if (idx==Len1 || !s2[idx]) break; 00376 cmp = tolower(*iter1)-tolower(s2[idx]); 00377 ++idx; 00378 ++iter1; 00379 } 00380 return cmp; 00381 } 00386 inline int strsicmp(const char *s1, const std::string& s2) { 00387 return -strsicmp(s2, s1); 00388 } 00389 00394 inline int strsicmp(const std::string& s1, const std::string& s2) { 00395 return strnicmp(s1, s2, gen_algo::min(s1.length(), s2.length())); 00396 } 00397 00399 00400 // ---------------------------------------- strichr 00401 00416 inline const char *strichr(const char *haystack, char needle) { 00417 needle = tolower(needle); 00418 while (1) { 00419 char c = tolower(*haystack++); 00420 if (c==needle) return haystack-1; 00421 if (!c) break; 00422 } 00423 return 0; 00424 } 00429 inline char *strichr(char *haystack, char needle) { 00430 return const_cast<char*>(strichr(const_cast<const char *>(haystack), needle)); 00431 } 00432 00434 00435 // ---------------------------------------- stristr 00436 00451 inline const char *stristr(const char *haystack, const char *needle) { 00452 const char *first = haystack; 00453 size_t len = strlen(needle); 00454 while (1) { 00455 first = strichr(first, needle[0]); 00456 if (!first) break; 00457 if (strnicmp(first, needle, len)==0) return first; 00458 ++first; 00459 } 00460 return 0; 00461 } 00466 inline char *stristr(char *haystack, const char* needle) { 00467 return const_cast<char*>(stristr(const_cast<const char *>(haystack), needle)); 00468 } 00469 00471 00472 // ---------------------------------------- strlwr/strupr 00473 00486 inline void strlwr(char *s) { 00487 size_t i = 0; 00488 while (1) { 00489 if ((s[i]=tolower(s[i]))==0) break; 00490 ++i; 00491 } 00492 } 00497 inline void strlwr(std::string& s) { 00498 size_t len = s.length(); 00499 std::string::iterator iter = s.begin(); 00500 while (len--) { 00501 *iter = tolower(*iter); 00502 ++iter; 00503 } 00504 } 00506 inline void strupr(char *s) { 00507 size_t i = 0; 00508 while (1) { 00509 if ((s[i]=toupper(s[i]))==0) break; 00510 ++i; 00511 } 00512 } 00513 00518 inline void strupr(std::string& s) { 00519 size_t len = s.length(); 00520 std::string::iterator iter = s.begin(); 00521 while (len--) { 00522 *iter = toupper(*iter); 00523 ++iter; 00524 } 00525 } 00526 00528 }; 00529 00531 namespace output { 00532 00539 void outf(std::ostream& out, const char *format, ...); 00540 00541 // /** @brief output with fixed width 00542 00543 // breaks long lines 00544 00545 // @param out output stream 00546 // @param s string to print 00547 // @param width width of output in columns 00548 // */ 00549 // void fixed_width_output(std::ostream& out, const std::string& s, size_t width); 00550 00559 void printIndented(std::ostream& out, unsigned indent, unsigned border, unsigned width, const std::string& s); 00560 00571 void printIndented(std::ostream& out, unsigned width, const std::string& left, const std::string& right); 00572 00573 }; 00574 }; 00575 00576 #endif //TOOLS_H
Contact me in case of errors or questions. This documentation is powered by . |
(C) 2000-2002 |