1 /* 2 * 3 * C++ Portable Types Library (PTypes) 4 * Version 2.1.1 Released 27-Jun-2007 5 * 6 * Copyright (C) 2001-2007 Hovik Melikyan 7 * 8 * http://www.melikyan.com/ptypes/ 9 * 10 */ 11 12 #include "ptypes.h" 13 14 15 PTYPES_BEGIN 16 17 lowercase(const char * p)18string ptdecl lowercase(const char* p) 19 { 20 // we rely on the function locase() which converts one single 21 // character to lower case. all locale specific things can be 22 // settled down in the future releases. 23 string r; 24 if (p != nil) 25 { 26 char* d = setlength(r, strlen(p)); 27 while (*p != 0) 28 *d++ = locase(*p++); 29 } 30 return r; 31 } 32 33 lowercase(const string & s)34string ptdecl lowercase(const string& s) 35 { 36 // this function does practically nothing if the string s 37 // contains no uppercase characters. once an uppercase character 38 // is encountered, the string is copied to another buffer and the 39 // rest is done as usual. 40 41 string r = s; 42 43 // a trick to get a non-const pointer without making 44 // the string unique 45 char* p = pchar(pconst(r)); 46 bool u = false; 47 int i = 0; 48 49 while (*p != 0) 50 { 51 char c = locase(*p); 52 // if the character went lowercase... 53 if (c != *p) 54 { 55 // if the resulting string r is not unique yet... 56 if (!u) 57 { 58 // ... make it unique and adjust the pointer p accordingly 59 // this is done only once. 60 p = unique(r) + i; 61 u = true; 62 } 63 *p = c; 64 } 65 p++; 66 i++; 67 } 68 69 return r; 70 } 71 72 73 PTYPES_END 74