Yiin

ChangeLetters

Jun 10th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
  2.     // No need to do anything if the limit is 0.
  3.     if (limit == 0)
  4.         return 0;
  5.    
  6.     new
  7.              sublen = strlen(search),
  8.              replen = strlen(replacement),
  9.         bool:packed = ispacked(string),
  10.              maxlen = maxlength,
  11.              len = strlen(string),
  12.              count = 0
  13.     ;
  14.    
  15.    
  16.     // "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
  17.     // Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
  18.     if (packed)
  19.         maxlen *= 4;
  20.    
  21.     // If the length of the substring is 0, we have nothing to look for..
  22.     if (!sublen)
  23.         return 0;
  24.    
  25.     // In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
  26.     while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
  27.         // Delete the string we found
  28.         strdel(string, pos, pos + sublen);
  29.        
  30.         len -= sublen;
  31.        
  32.         // If there's anything to put as replacement, insert it. Make sure there's enough room first.
  33.         if (replen && len + replen < maxlen) {
  34.             strins(string, replacement, pos, maxlength);
  35.            
  36.             pos += replen;
  37.             len += replen;
  38.         }
  39.        
  40.         // Is there a limit of number of replacements, if so, did we break it?
  41.         if (limit != -1 && ++count >= limit)
  42.             break;
  43.     }
  44.    
  45.     return count;
  46. }
  47.  
  48. ChangeLetters(source[]) {
  49.     new ret[MAX_PLAYER_NAME];
  50.     strcat(ret, source);
  51.     new lt_arr[][10] = {
  52.         "Ą","Č","Ę","Ė","Į","Š","Ų","Ū","Ž",
  53.         "ą","č","ę","ė","į","š","ų","ū","ž"
  54.     };
  55.     new en_arr[][10] = {
  56.         "A","C","E","E","I","S","U","U","Z",
  57.         "a","c","e","e","i","s","u","u","z"
  58.     };
  59.     for(new i; i < sizeof lt_arr; ++i) {
  60.         strreplace(ret, lt_arr[i], en_arr[i]);
  61.     }
  62.     return ret;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment