Advertisement
BigETI

c_string_h.inc

May 18th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.64 KB | None | 0 0
  1. /*
  2.     C like string functions made by BigETI © 2013
  3.     c_string_h.inc
  4.     You are allowed to edit this include!
  5.  
  6.     "strcpy" macro credits: http://forum.sa-mp.com/showpost.php?p=2532139&postcount=19
  7. */
  8. #include <string>
  9. #if defined _C_STRING_H_INCLUDED_
  10.     #endinput
  11. #endif
  12. #define _C_STRING_H_INCLUDED_
  13. #if !defined STRBRK_BUFF_LEN
  14.     #define STRBRK_BUFF_LEN (128)
  15. #endif
  16. #define __strcat_(%0,%1)        strcat(%0,%1)
  17. #define strncat(%0,%1,%2)       strmid(%0,%1,0,%2)
  18. stock strchr(const str[], chr)
  19. {
  20.     new str_len = strlen(str);
  21.     if(str_len <= 0) return 0;
  22.     for(new str_pos = 0; str_pos < str_len; str_pos++) if(str[str_pos] == chr) return str_pos+1;
  23.     return 0;
  24. }
  25. stock strrchr(const str[], chr)
  26. {
  27.     new str_len = strlen(str);
  28.     if(str_len <= 0) return 0;
  29.     for(new str_pos = str_len-1; str_pos >= 0; str_pos--) if(str[str_pos] == chr) return str_pos+1;
  30.     return 0;
  31. }
  32. #define __strcmp_(%0,%1)        strcmp(%0,%1,_,cellmax)
  33. #define strncmp(%0,%1,%2)       strcmp(%0,%1,_,%2)
  34. #define strcasecmp(%0,%1)       strcmp(%0,%1,true,cellmax)
  35. #define strncasecmp(%0,%1,%2)   strcmp(%0,%1,true,%2)
  36. #define strcpy(%0,%1)           strcat((%0[0]='\0',%0),%1)
  37. #define strncpy(%0,%1,%2)       strmid(%0,%1,0,%2,sizeof(%0))
  38. stock strpbrk(const str1[], const str2[])
  39. {
  40.     new buffer[STRBRK_BUFF_LEN] = "", buffer_pos = 0, str_len[2];
  41.     if((str_len[0] = strlen(str1)) <= 0 || (str_len[1] = strlen(str2)) <= 0) return buffer;
  42.     new str_pos[2];
  43.     for(str_pos[1] = 0; str_pos[1] < str_len[1]; str_pos[1]++)
  44.     {
  45.         for(str_pos[0] = 0; str_pos[0] < str_len[0]; str_pos[0]++)
  46.         {
  47.             if(str1[str_pos[0]] == str2[str_pos[1]])
  48.             {
  49.                 buffer[buffer_pos] = str1[str_pos[0]];
  50.                 if((++buffer_pos) >= (STRBRK_BUFF_LEN-1))
  51.                 {
  52.                     buffer[STRBRK_BUFF_LEN-1] = '\0';
  53.                     return buffer;
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     buffer[buffer_pos] = '\0';
  59.     return buffer;
  60. }
  61. stock strspn(const str1[], const str2[])
  62. {
  63.     new count = 0, str_len[2];
  64.     if((str_len[0] = strlen(str1)) <= 0 || (str_len[1] = strlen(str2)) <= 0) return 0;
  65.     new str_pos[2];
  66.     for(str_pos[0] = 0; str_pos[0] < str_len[0]; str_pos[0]++)
  67.     {
  68.         for(str_pos[1] = 0; str_pos[1] < str_len[1]; str_pos[1]++)
  69.         {
  70.             if(str1[str_pos[0]] == str2[str_pos[1]])
  71.             {
  72.                 count++;
  73.                 break;
  74.             }
  75.         }
  76.     }
  77.     return count;
  78. }
  79. stock strcspn(const str1[], const str2[])
  80. {
  81.     new str_len[2];
  82.     if((str_len[0] = strlen(str1)) <= 0 || (str_len[1] = strlen(str2)) <= 0) return 0;
  83.     new str_pos[2];
  84.     for(str_pos[0] = 0; str_pos[0] < str_len[0]; str_pos[0]++) for(str_pos[1] = 0; str_pos[1] < str_len[1]; str_pos[1]++) if(str1[str_pos[0]] == str2[str_pos[1]]) return str_pos[0]+1;
  85.     return 0;
  86. }
  87. #define strstr(%0,%1)           strfind(%0,%1,false,_)
  88. #define strcasestr(%0,%1)       strfind(%0,%1,true,_)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement