// File: gersteinlab[P]MUSIC/src/ms_ansi_string.cpp
// Function: t_string::tokenize_by_str(char const*)
t_string_tokens* t_string::tokenize_by_str(const char* delimiter_string)
{
	t_string_tokens* token_vector = new vector<t_string*>();

	// Current token is nothing at the beginning.
	t_string* current_token = new t_string();

	// Go over the obj_string.
	int str_len = this->length();
	for(int i_str = 0; i_str < str_len; i_str++)
	{
		//printf("Processing: %c\n", this->x(i_str));
		// This is set to true if i_str'th character is a delimiter.
		bool is_delimiter = false;

		int i_str_search = i_str;
		// Check if current character in the string is a delimiter character.
		for(int i_del = 0; i_del < string_length(delimiter_string); i_del++)
		{
			if(i_str_search == this->length() || this->x(i_str_search) != delimiter_string[i_del])
			{
				is_delimiter = false;
				break;
			}				

			// If the current substring matches the delimiter string, set delimiter to true.
			if(i_del == string_length(delimiter_string) - 1)
			{
				// Push current token to vector (if it is appropriate for pushing.) and start a new token.
				// Push the previous token. Cannot push the next token since there is no way of knowing that
				// it is a valid token beforehand.
				if(current_token != NULL && current_token->length() != 0)
				{
					// Push this token to token vector.
					//printf("pushing %s\n", current_token->obj_string);
					token_vector->push_back(current_token);

					// Do not free current token now, allocate a new one.
					current_token = new t_string();
				}
				else // The token is not a good token (i.e., an empty token), free its memory and 
				{
					current_token->empty();
				}

				is_delimiter = true;

				// Note that the last char that is at i_str_search is the last char
				// of delimiter string but that will be jumped over by for loop.
				// This is the same in tokenize_by_chars function.
				i_str = i_str_search;
				break;
			}

			// Increment substring counter.
			i_str_search++;

		} // delimiter char loop.

		// If this character is not a delimiter, concatenate the character to the current_token.
		if(!is_delimiter)
		{
			current_token->concat_char(this->x(i_str));
		}

	} // string char loop.

	// The last token is added to the tokens vector after whole loop is finished.
	if(current_token != NULL && current_token->length() != 0)
	{
		// Push this token to token vector.
		token_vector->push_back(current_token);
		//printf("pushing %s\n", current_token->obj_string);
	}
	else
	{
		delete(current_token);
	}

	return(token_vector);
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movq %rsi, 0x78(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x18, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, 0x38(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0x4b800
movq 0x38(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x8, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
callq 0x474d0
jmp 0x4a0ec
movq 0x30(%rsp), %rdi
movq 0x40(%rsp), %rax
movq %rax, 0x68(%rsp)
callq 0x481a0
movl %eax, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x58(%rsp), %eax
jge 0x4a2bf
movb $0x0, 0x53(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl $0x0, 0x48(%rsp)
movl 0x48(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x78(%rsp), %rdi
callq 0x47640
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x4a281
movq 0x30(%rsp), %rdi
movl 0x4c(%rsp), %eax
movl %eax, 0x28(%rsp)
callq 0x481a0
movl %eax, %ecx
movl 0x28(%rsp), %eax
cmpl %ecx, %eax
je 0x4a18e
movq 0x30(%rsp), %rdi
movl 0x4c(%rsp), %esi
callq 0x481b0
movsbl (%rax), %eax
movq 0x78(%rsp), %rcx
movslq 0x48(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0x4a1ba
movb $0x0, 0x53(%rsp)
jmp 0x4a281
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
movl $0x8, %esi
callq 0x21a0
jmp 0x4a31f
movl 0x48(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x78(%rsp), %rdi
callq 0x47640
movl %eax, %ecx
movl 0x24(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x4a266
cmpq $0x0, 0x68(%rsp)
je 0x4a24d
movq 0x68(%rsp), %rdi
callq 0x481a0
cmpl $0x0, %eax
je 0x4a24d
movq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x4b810
movl $0x8, %edi
callq 0x2190
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x18(%rsp)
callq 0x474d0
jmp 0x4a21f
movq 0x18(%rsp), %rax
movq %rax, 0x68(%rsp)
jmp 0x4a257
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
movl $0x8, %esi
callq 0x21a0
jmp 0x4a31f
movq 0x68(%rsp), %rdi
callq 0x47550
movb $0x1, 0x53(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x4a281
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x4a12f
testb $0x1, 0x53(%rsp)
jne 0x4a2ad
movq 0x30(%rsp), %rdi
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0x54(%rsp), %esi
callq 0x481b0
movq 0x10(%rsp), %rdi
movsbl (%rax), %esi
callq 0x481d0
jmp 0x4a2af
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x4a10c
cmpq $0x0, 0x68(%rsp)
je 0x4a2e7
movq 0x68(%rsp), %rdi
callq 0x481a0
cmpl $0x0, %eax
je 0x4a2e7
movq 0x70(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x4b810
jmp 0x4a312
movq 0x68(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x4a310
movq 0x8(%rsp), %rdi
callq 0x47f70
movq 0x8(%rsp), %rdi
movl $0x8, %esi
callq 0x21a0
jmp 0x4a312
movq 0x70(%rsp), %rax
addq $0x88, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x2300
nopl (%rax)