// File: craigbarstow[P]cmd-line-text-editor/text_editor.cc
// Function: find_and_replace(text_ed&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool find_and_replace(text_ed & T, string cl) { 
  bool error_state=false;
  string aline,m_string,n_string, word, second_word;
  int i = 0,m=0,n=0,p=1,m_begin, m_end,m_length, n_begin, n_end, n_length, document_length, word_start, word_end, second_word_start, second_word_end, count=0;
    //SECTION TO ISOLATE INTEGERS FROM THE GIVEN STRING
  if (cl.find_first_not_of(" ",1)!=string::npos){ //section to isolate first integer from string
    m_begin= cl.find_first_not_of(" ",1);
    m_end = cl.find_first_of(" ", m_begin+1); 
    m_length = m_end-m_begin;
    m_string=cl.substr(m_begin,m_length);
    if (istringstream(m_string) >> m) { //if first substring can be converted to an integer, increment the count to show it is present
      count++;
    }
    if (cl.find_first_not_of(" ", m_end+1)!=string::npos){ //section to isolate second integer from the string
      n_begin = cl.find_first_not_of(" ", m_end+1);
      n_end = cl.find_first_of(" ",n_begin);
      n_length = n_end-n_begin;
      n_string=cl.substr(n_begin,n_length);
      if (istringstream(n_string) >> n){ //if the second substring can be converted to an integer, increment the count to show it is present
	count++;
      }
    }
  }
   //SECTION TO MAKE SURE THE INTEGER VALUES MAKE SENSE
    document_length = T.get_last_line();
    if ((m<=0 || m>document_length)&& count!=0){ //if first integer is less than the starting line number, set it to the starting line number
      error_state=true;
    }
    if (n>document_length) { //if ending integer value is greater than the document length, toggle error state to true
      error_state=true;
    }
    if (n<m && count==2){ //if the second integer is greater than the first, toggle error state on
      error_state=true;
    }

    //SECTION TO ISOLATE WORDS FROM THE STRING
    if (cl.find_first_of("/")!=string::npos){ //portion to isolate first word
      word_start = cl.find_first_of("/")+1;
      word_end = cl.find_first_of("/",word_start);
      word = cl.substr(word_start,word_end-word_start);
    } else {error_state=true;} //if first word can't be found, toggle error state to true
    if (cl.find_first_of("/",word_end+1)!=string::npos){ //portion to isolate the replacement word
      second_word_start = cl.find_first_not_of("/",word_end);
      second_word_end = cl.find_first_of("/",second_word_start);
      second_word = cl.substr(second_word_start, second_word_end-second_word_start);
    } else {error_state=true;} //if second word can't be found, toggle error state to true
    if (!error_state) {
      switch (count) {
      case 0: //if no integers present, find and replace throughout document
	T.replace(word,second_word,1,document_length); //if no integer present, replace old word with new word throughout the document
	break;
      case 1: //if one integer is present, find and replace on just that line
	T.replace(word,second_word,m,m);
	break;
      case 2: //if two integers present, replace old word with new word from line m to line n
	T.replace(word,second_word,m,n);
	break;
      }
      return true;
    } else {
      return false; //if error state is true, return false
    }
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x5e0, %rsp            # imm = 0x5E0
movq %rsi, -0x560(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x32d0
leaq -0x60(%rbp), %rdi
callq 0x32d0
leaq -0x80(%rbp), %rdi
callq 0x32d0
leaq -0xa0(%rbp), %rdi
callq 0x32d0
leaq -0xc0(%rbp), %rdi
callq 0x32d0
movq -0x560(%rbp), %rdi
movl $0x0, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movl $0x1, -0xd0(%rbp)
movl $0x0, -0x100(%rbp)
leaq 0x2f4d(%rip), %rsi      # 0x93ce
movl $0x1, %edx
callq 0x3140
cmpq $-0x1, %rax
je 0x676f
movq -0x560(%rbp), %rdi
leaq 0x2f2b(%rip), %rsi      # 0x93ce
movq %rsi, -0x568(%rbp)
movl $0x1, %edx
callq 0x3140
movq -0x568(%rbp), %rsi
movq -0x560(%rbp), %rdi
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
incl %eax
movslq %eax, %rdx
callq 0x3130
movq -0x560(%rbp), %rsi
movl %eax, -0xd8(%rbp)
movl -0xd8(%rbp), %eax
movl -0xd4(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0xdc(%rbp)
movslq -0xd4(%rbp), %rdx
movslq -0xdc(%rbp), %rcx
leaq -0x120(%rbp), %rdi
callq 0x3200
jmp 0x6515
leaq -0x60(%rbp), %rdi
movq %rdi, -0x570(%rbp)
leaq -0x120(%rbp), %rsi
movq %rsi, -0x578(%rbp)
callq 0x3280
movq -0x578(%rbp), %rdi
callq 0x3160
movq -0x570(%rbp), %rsi
leaq -0x2b0(%rbp), %rdi
movl $0x8, %edx
callq 0x3340
jmp 0x6559
leaq -0x2b0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x7690
movq %rax, -0x580(%rbp)
jmp 0x6575
movq -0x580(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x32a0
movb %al, -0x581(%rbp)
jmp 0x6593
leaq -0x2b0(%rbp), %rdi
callq 0x3120
movb -0x581(%rbp), %al
testb $0x1, %al
jne 0x65ab
jmp 0x65f6
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
jmp 0x65f6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
jmp 0x6c20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
leaq -0x2b0(%rbp), %rdi
callq 0x3120
jmp 0x6c20
movq -0x560(%rbp), %rdi
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
leaq 0x2dbe(%rip), %rsi      # 0x93ce
callq 0x3140
cmpq $-0x1, %rax
je 0x676d
movq -0x560(%rbp), %rdi
movl -0xd8(%rbp), %eax
incl %eax
movslq %eax, %rdx
leaq 0x2d96(%rip), %rsi      # 0x93ce
movq %rsi, -0x590(%rbp)
callq 0x3140
movq -0x590(%rbp), %rsi
movq -0x560(%rbp), %rdi
movl %eax, -0xe0(%rbp)
movslq -0xe0(%rbp), %rdx
callq 0x3130
movq -0x560(%rbp), %rsi
movl %eax, -0xe4(%rbp)
movl -0xe4(%rbp), %eax
movl -0xe0(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0xe8(%rbp)
movslq -0xe0(%rbp), %rdx
movslq -0xe8(%rbp), %rcx
leaq -0x2d0(%rbp), %rdi
callq 0x3200
jmp 0x66a1
leaq -0x80(%rbp), %rdi
movq %rdi, -0x598(%rbp)
leaq -0x2d0(%rbp), %rsi
movq %rsi, -0x5a0(%rbp)
callq 0x3280
movq -0x5a0(%rbp), %rdi
callq 0x3160
movq -0x598(%rbp), %rsi
leaq -0x450(%rbp), %rdi
movl $0x8, %edx
callq 0x3340
jmp 0x66e5
leaq -0x450(%rbp), %rdi
leaq -0xcc(%rbp), %rsi
callq 0x7690
movq %rax, -0x5a8(%rbp)
jmp 0x6701
movq -0x5a8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x32a0
movb %al, -0x5a9(%rbp)
jmp 0x671f
leaq -0x450(%rbp), %rdi
callq 0x3120
movb -0x5a9(%rbp), %al
testb $0x1, %al
jne 0x6737
jmp 0x676b
movl -0x100(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x100(%rbp)
jmp 0x676b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
leaq -0x450(%rbp), %rdi
callq 0x3120
jmp 0x6c20
jmp 0x676d
jmp 0x676f
movq -0x10(%rbp), %rdi
callq 0x82c0
movl %eax, -0x5b0(%rbp)
jmp 0x6780
movl -0x5b0(%rbp), %eax
movl %eax, -0xec(%rbp)
cmpl $0x0, -0xc8(%rbp)
jle 0x67a3
movl -0xc8(%rbp), %eax
cmpl -0xec(%rbp), %eax
jle 0x67b0
cmpl $0x0, -0x100(%rbp)
je 0x67b0
movb $0x1, -0x19(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xec(%rbp), %eax
jle 0x67c2
movb $0x1, -0x19(%rbp)
movl -0xcc(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jge 0x67dd
cmpl $0x2, -0x100(%rbp)
jne 0x67dd
movb $0x1, -0x19(%rbp)
movq -0x560(%rbp), %rdi
leaq 0x2c1a(%rip), %rsi      # 0x9405
xorl %eax, %eax
movl %eax, %edx
callq 0x3130
cmpq $-0x1, %rax
je 0x688e
movq -0x560(%rbp), %rdi
leaq 0x2bf9(%rip), %rsi      # 0x9405
movq %rsi, -0x5b8(%rbp)
xorl %eax, %eax
movl %eax, %edx
callq 0x3130
movq -0x5b8(%rbp), %rsi
movq -0x560(%rbp), %rdi
incl %eax
movl %eax, -0xf0(%rbp)
movslq -0xf0(%rbp), %rdx
callq 0x3130
movq -0x560(%rbp), %rsi
movl %eax, -0xf4(%rbp)
movslq -0xf0(%rbp), %rdx
movl %edx, %ecx
movl -0xf4(%rbp), %eax
subl %ecx, %eax
movslq %eax, %rcx
leaq -0x470(%rbp), %rdi
callq 0x3200
jmp 0x686d
leaq -0xa0(%rbp), %rdi
leaq -0x470(%rbp), %rsi
callq 0x3280
leaq -0x470(%rbp), %rdi
callq 0x3160
jmp 0x6892
movb $0x1, -0x19(%rbp)
movq -0x560(%rbp), %rdi
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rdx
leaq 0x2b59(%rip), %rsi      # 0x9405
callq 0x3130
cmpq $-0x1, %rax
je 0x694c
movq -0x560(%rbp), %rdi
movslq -0xf4(%rbp), %rdx
leaq 0x2b35(%rip), %rsi      # 0x9405
movq %rsi, -0x5c0(%rbp)
callq 0x3140
movq -0x5c0(%rbp), %rsi
movq -0x560(%rbp), %rdi
movl %eax, -0xf8(%rbp)
movslq -0xf8(%rbp), %rdx
callq 0x3130
movq -0x560(%rbp), %rsi
movl %eax, -0xfc(%rbp)
movslq -0xf8(%rbp), %rdx
movl %edx, %ecx
movl -0xfc(%rbp), %eax
subl %ecx, %eax
movslq %eax, %rcx
leaq -0x490(%rbp), %rdi
callq 0x3200
jmp 0x692b
leaq -0xc0(%rbp), %rdi
leaq -0x490(%rbp), %rsi
callq 0x3280
leaq -0x490(%rbp), %rdi
callq 0x3160
jmp 0x6950
movb $0x1, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
jne 0x6bd1
movl -0x100(%rbp), %eax
movl %eax, -0x5c4(%rbp)
testl %eax, %eax
je 0x6991
jmp 0x696c
movl -0x5c4(%rbp), %eax
subl $0x1, %eax
je 0x6a4e
jmp 0x697d
movl -0x5c4(%rbp), %eax
subl $0x2, %eax
je 0x6b09
jmp 0x6bc1
movq -0x10(%rbp), %rax
movq %rax, -0x5d0(%rbp)
leaq -0x4b0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x30e0
jmp 0x69b1
leaq -0x4d0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x30e0
jmp 0x69c6
movq -0x5d0(%rbp), %rdi
movl -0xec(%rbp), %r8d
leaq -0x4b0(%rbp), %rsi
leaq -0x4d0(%rbp), %rdx
movl $0x1, %ecx
callq 0x8860
jmp 0x69ee
leaq -0x4d0(%rbp), %rdi
callq 0x3160
leaq -0x4b0(%rbp), %rdi
callq 0x3160
jmp 0x6bc1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
jmp 0x6a3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
leaq -0x4d0(%rbp), %rdi
callq 0x3160
leaq -0x4b0(%rbp), %rdi
callq 0x3160
jmp 0x6c20
movq -0x10(%rbp), %rax
movq %rax, -0x5d8(%rbp)
leaq -0x4f0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x30e0
jmp 0x6a6e
leaq -0x510(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x30e0
jmp 0x6a83
movq -0x5d8(%rbp), %rdi
movl -0xc8(%rbp), %r8d
leaq -0x4f0(%rbp), %rsi
leaq -0x510(%rbp), %rdx
movl %r8d, %ecx
callq 0x8860
jmp 0x6aa9
leaq -0x510(%rbp), %rdi
callq 0x3160
leaq -0x4f0(%rbp), %rdi
callq 0x3160
jmp 0x6bc1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
jmp 0x6af8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
leaq -0x510(%rbp), %rdi
callq 0x3160
leaq -0x4f0(%rbp), %rdi
callq 0x3160
jmp 0x6c20
movq -0x10(%rbp), %rax
movq %rax, -0x5e0(%rbp)
leaq -0x530(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x30e0
jmp 0x6b29
leaq -0x550(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x30e0
jmp 0x6b3e
movq -0x5e0(%rbp), %rdi
movl -0xc8(%rbp), %ecx
movl -0xcc(%rbp), %r8d
leaq -0x530(%rbp), %rsi
leaq -0x550(%rbp), %rdx
callq 0x8860
jmp 0x6b67
leaq -0x550(%rbp), %rdi
callq 0x3160
leaq -0x530(%rbp), %rdi
callq 0x3160
jmp 0x6bc1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
jmp 0x6bb3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x128(%rbp)
movl %eax, -0x12c(%rbp)
leaq -0x550(%rbp), %rdi
callq 0x3160
leaq -0x530(%rbp), %rdi
callq 0x3160
jmp 0x6c20
movb $0x1, -0x1(%rbp)
movl $0x1, -0x554(%rbp)
jmp 0x6bdf
movb $0x0, -0x1(%rbp)
movl $0x1, -0x554(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x3160
leaq -0xa0(%rbp), %rdi
callq 0x3160
leaq -0x80(%rbp), %rdi
callq 0x3160
leaq -0x60(%rbp), %rdi
callq 0x3160
leaq -0x40(%rbp), %rdi
callq 0x3160
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x5e0, %rsp            # imm = 0x5E0
popq %rbp
retq
leaq -0xc0(%rbp), %rdi
callq 0x3160
leaq -0xa0(%rbp), %rdi
callq 0x3160
leaq -0x80(%rbp), %rdi
callq 0x3160
leaq -0x60(%rbp), %rdi
callq 0x3160
leaq -0x40(%rbp), %rdi
callq 0x3160
movq -0x128(%rbp), %rdi
callq 0x3380
nop