Advertisement
Tkap1

Untitled

May 8th, 2024
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1.  
  2. // usage
  3. {
  4.     char* str = "athano azen";
  5.     char entity_a[128];
  6.     char entity_b[128];
  7.     char* curr = str;
  8.     curr = parse_identifier(curr, entity_a);
  9.     if(!curr) {
  10.         // DIDNT FIND AN IDENTIFIER AKA ENTITY NAME IN THIS CASE
  11.         return;
  12.     }
  13.     curr = parse_identifier(curr, entity_b);
  14.     if(!curr) {
  15.         // FOUND ONE NAME BUT NOT TWO
  16.         return;
  17.     }
  18.     // entity_a is "athano"
  19.     // entity_b is "azen"
  20. }
  21.  
  22. char* parse_identifier(char* source, char* fill_me_up_big_boy)
  23. {
  24.     // skip spaces
  25.     while(*source == ' ') {
  26.         source += 1;
  27.     }
  28.  
  29.     char* result = null;
  30.     if(!begins_identifier(source[0])) { return result; }
  31.     int i = 1;
  32.     while(true) {
  33.         if(!continues_identifier(source[i])) {
  34.             memcpy(fill_me_up_big_boy, source, i);
  35.             fill_me_up_big_boy[i] = 0;
  36.             result = &source[i];
  37.             break;
  38.         }
  39.         i += 1;
  40.     }
  41.     return result;
  42. }
  43.  
  44.  
  45.  
  46.  
  47. /////////
  48. bool is_number(char c)
  49. {
  50.     return c >= '0' && c <= '9';
  51. }
  52.  
  53. bool is_alpha(char c)
  54. {
  55.     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ;
  56. }
  57.  
  58. bool is_alphanum(char c)
  59. {
  60.     return is_alpha(c) || is_number(c);
  61. }
  62.  
  63. bool begins_identifier(char c)
  64. {
  65.     return is_alphanum(c) || c == '_'
  66. }
  67.  
  68. bool continues_identifier(char c)
  69. {
  70.     return begins_identifier(c) || is_number(c);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement