Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // usage
- {
- char* str = "athano azen";
- char entity_a[128];
- char entity_b[128];
- char* curr = str;
- curr = parse_identifier(curr, entity_a);
- if(!curr) {
- // DIDNT FIND AN IDENTIFIER AKA ENTITY NAME IN THIS CASE
- return;
- }
- curr = parse_identifier(curr, entity_b);
- if(!curr) {
- // FOUND ONE NAME BUT NOT TWO
- return;
- }
- // entity_a is "athano"
- // entity_b is "azen"
- }
- char* parse_identifier(char* source, char* fill_me_up_big_boy)
- {
- // skip spaces
- while(*source == ' ') {
- source += 1;
- }
- char* result = null;
- if(!begins_identifier(source[0])) { return result; }
- int i = 1;
- while(true) {
- if(!continues_identifier(source[i])) {
- memcpy(fill_me_up_big_boy, source, i);
- fill_me_up_big_boy[i] = 0;
- result = &source[i];
- break;
- }
- i += 1;
- }
- return result;
- }
- /////////
- bool is_number(char c)
- {
- return c >= '0' && c <= '9';
- }
- bool is_alpha(char c)
- {
- return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ;
- }
- bool is_alphanum(char c)
- {
- return is_alpha(c) || is_number(c);
- }
- bool begins_identifier(char c)
- {
- return is_alphanum(c) || c == '_'
- }
- bool continues_identifier(char c)
- {
- return begins_identifier(c) || is_number(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement