Advertisement
Guest User

strmanip.h

a guest
Feb 24th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 KB | None | 0 0
  1. #ifndef STRMANIP
  2. #define STRMANIP
  3.  
  4. #include <string.h>
  5. #include <stdbool.h>
  6. #include <ctype.h>
  7.  
  8. #include "memmanage.h"
  9.  
  10. char *str_check(char *s)
  11. {
  12.     if (!s) {
  13.         s = mem_alloc(1);
  14.         *s = '\0';
  15.     }
  16.     return s;
  17. }
  18.  
  19. char *str_insert(char *s1, char* s2, unsigned long p)
  20. {
  21.     s1 = str_check(s1);
  22.     s2 = str_check(s2);
  23.  
  24.     unsigned long len1 = strlen(s1);
  25.     unsigned long len2 = strlen(s2);
  26.     char *s_final = mem_alloc(len1 + len2 + 1 * sizeof(char));
  27.  
  28.     unsigned long i = 0;
  29.     unsigned long j = 0;
  30.     unsigned long k = 0;
  31.  
  32.     p = len1 ? p % (len1 + 1) : 0;
  33.  
  34.     while (i < p)
  35.         s_final[i++] = s1[j++];
  36.  
  37.     while (k < len2)
  38.         s_final[i++] = s2[k++];
  39.  
  40.     while (j < len1)
  41.         s_final[i++] = s1[j++];
  42.  
  43.     s_final[i] = '\0';
  44.     return s_final;
  45. }
  46.  
  47. char *str_remove(char *s, unsigned long p1, unsigned long p2)
  48. {
  49.     s = str_check(s);
  50.  
  51.     if (p1 > p2) {
  52.         unsigned long tmp = p1;
  53.         p1 = p2;
  54.         p2 = tmp;
  55.     }
  56.  
  57.     unsigned long len = strlen(s);
  58.  
  59.     p1 = len ? p1 % len : 0;
  60.     p2 = len ? p2 % len : 0;
  61.  
  62.     len -= p2 - p1;
  63.     char *s_final = mem_alloc((len + 1) * sizeof(char));
  64.  
  65.     unsigned long i = 0;
  66.     unsigned long j = 0;
  67.  
  68.     while (i < p1)
  69.         s_final[i++] = s[j++];
  70.  
  71.     j = p2 + 1;
  72.  
  73.     while (i < len)
  74.         s_final[i++] = s[j++];
  75.  
  76.     s_final[i] = '\0';
  77.     return s_final;
  78. }
  79.  
  80. char *str_append(char *s1, char *s2)
  81. {
  82.     s1 = str_check(s1);
  83.     return str_insert(s1, s2, strlen(s1));
  84. }
  85.  
  86. char *str_push(char *s1, char c)
  87. {
  88.     char* s2 = mem_alloc(2 * sizeof(char));
  89.  
  90.     s2[0] = c;
  91.     s2[1] = '\0';
  92.  
  93.     return str_append(s1, s2);
  94. }
  95.  
  96. char str_pop(char *s)
  97. {
  98.     s = str_check(s);
  99.  
  100.     unsigned long len = strlen(s);
  101.     unsigned long i = len ? len - 1 : 0;
  102.     char c = s[i];
  103.  
  104.     s[i] = '\0';
  105.     return c;
  106. }
  107.  
  108. char *str_input(void)
  109. {
  110.     char buffer[1024] = { 0 };
  111.     char *s = NULL;
  112.     unsigned long cap = 0;
  113.     unsigned int i = 0;
  114.     char c = '\0';
  115.  
  116.     while ((c = getchar()) != '\n' && c != EOF) {
  117.         buffer[i++] = c;
  118.         buffer[i] = '\0';
  119.  
  120.         if (i == 1024) {
  121.             s = mem_realloc(s, (cap += 1024) * sizeof(char));
  122.  
  123.             for (unsigned int j = 0; j < i; ++j)
  124.                 s[cap - i + j] = buffer[j];
  125.  
  126.             memset(buffer, '\0', 1024);
  127.             i = 0;
  128.         }
  129.     }
  130.  
  131.     if (i) {
  132.         s = mem_realloc(s, (cap += ++i));
  133.         for (unsigned int j = 0; j < i - 1; ++j)
  134.             s[cap - i + j] = buffer[j];
  135.     }
  136.  
  137.     if (s) s[i - 1] = '\0';
  138.     return s;
  139. }
  140.  
  141. unsigned int str_to_int_array(char *s, int **a)
  142. {
  143.     s = str_push(s, ' '); // The non-digit character makes the loop clean and stores the number
  144.     unsigned long len = strlen(s);
  145.     int count = 0;
  146.     int cap = 0;
  147.     int n = 0;
  148.     _Bool storing_n = false;
  149.  
  150.     for (unsigned int i = 0; i < len; ++i) {
  151.         if (isdigit(s[i])) {
  152.             n = n * 10 + s[i] - '0';
  153.             storing_n = true;
  154.         } else if (storing_n) {
  155.             if (count == cap)
  156.                 (*a) = mem_realloc((*a), (cap += 256) * sizeof(int));
  157.  
  158.             (*a)[count++] = n;
  159.             n = 0;
  160.             storing_n = false;
  161.         }
  162.     }
  163.  
  164.     return count;
  165. }
  166.  
  167. char *str_word(int n, char *s)
  168. {
  169.     char *w = NULL;
  170.     int i = 0;
  171.  
  172.     // Skip words until n
  173.     while (n--) {
  174.         while (isalnum(s[i]))
  175.             ++i;
  176.         while (!isalnum(s[i]) && s[i] != '\0')
  177.             ++i;
  178.     }
  179.  
  180.     int len = 0;
  181.     int cap = 0;
  182.  
  183.     while (isalnum(s[i]) && s[i] != '\0') {
  184.         if (len == cap)
  185.             w = mem_realloc(w, (cap += 20) * sizeof(char));
  186.  
  187.         w[len++] = s[i++];
  188.     }
  189.  
  190.     if (w != NULL) w[len] = '\0';
  191.     return w;
  192. }
  193.  
  194. char *str_literal(char *str)
  195. {
  196.     size_t len = strlen(str);
  197.     char *new = mem_alloc((len + 1) * sizeof(char));
  198.  
  199.     memcpy(new, str, len);
  200.  
  201.     return new;
  202. }
  203.  
  204. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement