Advertisement
Dimenticare

Splitting strings

Nov 22nd, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// ds_queue split(String s, char delimiter);
  2. // Will only recognize double-quotation mark for block quotes, not single-quotation marks.
  3. //      Shouldn't be too difficult to account for those too, if you want to.
  4. // You can pass a multiple-character string as the delimiter but it'll never get recognized
  5. //      beacuse string_char_at will never equal a multi-character delimiter.
  6.  
  7. var base=argument0;                             // Base string
  8. var delimiter=argument1;                        // Character to split around
  9. var inline=false;                               // inside a block quote?
  10. var queue=ds_queue_create();                    // Contains the individual words
  11. var tn="";                                      // temporary substring
  12.  
  13. base=base+delimiter;                            // lazy way of ensuring the last term in the list does not get skipped
  14.  
  15. for (var i=1; i<=string_length(base); i++){     // for each character in the string:
  16.     var c=string_char_at(base, i);              //      Current character
  17.     if (string_char_at(base, i-1)=="\"){        //      If the previous character is a backslash, bypass the other checks
  18.         tn=string_copy(tn, 1, string_length(tn)-1);     // and remove the backslash
  19.     } else if (c=='"'){                         //      If double quotation mark:
  20.         if (inline){                            //          If already inside a block, end the block
  21.             inline=false;
  22.         } else {                                //          If not already inside a block, start a block
  23.             inline=true;
  24.         }
  25.     } else if (c==delimiter&&!inline){          //      Delimiter met and not inside a block, enqueue and reset the substring
  26.         ds_queue_enqueue(queue, tn);
  27.         tn="";
  28.     } else {                                    // Just an ordinary character, add it to the substring
  29.         tn=tn+c;
  30.     }
  31. }
  32.  
  33. return queue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement