// File: akdess[P]BAFExtract/src/ansi_string.cpp
// Function: t_string::tokenize_by_str(char*, char const*)
t_string_tokens* t_string::tokenize_by_str(char* string, 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 = t_string::string_length(string);
	for(int i_str = 0; i_str < str_len; 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 == str_len || 
				string[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.
					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(string[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 $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl $0x18, %edi
callq 0x2180
movq %rax, %rdi
movq %rdi, 0x18(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
callq 0xd800
movq 0x18(%rsp), %rax
movq %rax, 0x50(%rsp)
movl $0x8, %edi
callq 0x2180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x20(%rsp)
callq 0x8630
jmp 0xbd79
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x60(%rsp), %rdi
callq 0x87e0
movl %eax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movl 0x34(%rsp), %eax
cmpl 0x38(%rsp), %eax
jge 0xbf2a
movb $0x0, 0x33(%rsp)
movl 0x34(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x58(%rsp), %rdi
callq 0x87e0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0xbef9
movl 0x2c(%rsp), %eax
cmpl 0x38(%rsp), %eax
je 0xbe06
movq 0x60(%rsp), %rax
movslq 0x2c(%rsp), %rcx
movsbl (%rax,%rcx), %eax
movq 0x58(%rsp), %rcx
movslq 0x28(%rsp), %rdx
movsbl (%rcx,%rdx), %ecx
cmpl %ecx, %eax
je 0xbe32
movb $0x0, 0x33(%rsp)
jmp 0xbef9
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0x8, %esi
callq 0x2190
jmp 0xbf84
movl 0x28(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x58(%rsp), %rdi
callq 0x87e0
movl %eax, %ecx
movl 0x10(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xbede
cmpq $0x0, 0x48(%rsp)
je 0xbec5
movq 0x48(%rsp), %rdi
callq 0x9920
cmpl $0x0, %eax
je 0xbec5
movq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xd810
movl $0x8, %edi
callq 0x2180
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
callq 0x8630
jmp 0xbe97
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
jmp 0xbecf
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x40(%rsp)
movl %eax, 0x3c(%rsp)
movl $0x8, %esi
callq 0x2190
jmp 0xbf84
movq 0x48(%rsp), %rdi
callq 0x86b0
movb $0x1, 0x33(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, 0x34(%rsp)
jmp 0xbef9
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0xbdbc
testb $0x1, 0x33(%rsp)
jne 0xbf18
movq 0x48(%rsp), %rdi
movq 0x60(%rsp), %rax
movslq 0x34(%rsp), %rcx
movsbl (%rax,%rcx), %esi
callq 0x9800
jmp 0xbf1a
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0xbd99
cmpq $0x0, 0x48(%rsp)
je 0xbf52
movq 0x48(%rsp), %rdi
callq 0x9920
cmpl $0x0, %eax
je 0xbf52
movq 0x50(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xd810
jmp 0xbf7a
movq 0x48(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xbf78
movq (%rsp), %rdi
callq 0x95f0
movq (%rsp), %rdi
movl $0x8, %esi
callq 0x2190
jmp 0xbf7a
movq 0x50(%rsp), %rax
addq $0x68, %rsp
retq
movq 0x40(%rsp), %rdi
callq 0x22d0
nop