Dimenticare

split(string)

Jan 7th, 2016
1,608
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. // You can pass a multiple-character string as the delimiter but it'll never get recognized
  4. //      because string_char_at will never equal a multi-character delimiter.
  5.  
  6. var base=argument[0];                           // Base string
  7. if (argument_count==1)
  8.     var delimiter=",";
  9. else
  10.     var delimiter=argument[1];                  // Character to split around, default a comma
  11. var inline=false;                               // inside a block quote?
  12. var queue=ds_queue_create();                    // Contains the individual words
  13. var tn="";                                      // temporary substring
  14.  
  15. base=base+delimiter;                            // lazy way of ensuring the last term in the list does not get skipped
  16.  
  17. for (var i=1; i<=string_length(base); i++){     // for each character in the string:
  18.     var c=string_char_at(base, i);              //      Current character
  19.     if (string_char_at(base, i-1)=="\"){        //      If the previous character is a backslash, bypass the other checks
  20.         tn=string_copy(tn, 1, string_length(tn)-1);     // and remove the backslash
  21.         tn=tn+c;
  22.     } else if (c=='"'){                         //      If double quotation mark:
  23.         if (inline){                            //          If already inside a block, end the block
  24.             inline=false;
  25.         } else {                                //          If not already inside a block, start a block
  26.             inline=true;
  27.         }
  28.     } else if (c==delimiter&&!inline){          //      Delimiter met and not inside a block, enqueue and reset the substring
  29.         ds_queue_enqueue(queue, tn);
  30.         tn="";
  31.     } else {                                    // Just an ordinary character, add it to the substring
  32.         tn=tn+c;
  33.     }
  34. }
  35.  
  36. return queue;
Advertisement
Add Comment
Please, Sign In to add comment