// File: /workspace/github_cpp_makefile_star2_O3/iZhangHui[P]CppPrimer/CH03_StringsVectorsAndArrays/examples/char_repl.cc
// Function: main
int main()
{
	string str("some string"), orig = str;
	if (!str.empty())          // make sure there's a character to print
		cout << str[0] << endl;// print the first character in str

	if (!str.empty())       // make sure there's a character in str[0]
		// assign a new value to the first character in str
		str[0] = toupper(str[0]);
	cout << str << endl;

	str = orig; // restore str to its original value

	// equivalent code using iterators instead of subscripts
	if (str.begin() != str.end()) { // make sure str is not empty
		auto it = str.begin();  // it denotes the first character in str
		*it = toupper(*it);     // make that character uppercase
	}
	cout << str << endl;

	str = orig; // restore str to its original value

	// four wasy to capitalize first word in str:
	// 1. for loop with subscripts
	for (decltype(str.size()) index = 0; 
		 index != str.size() && !isspace(str[index]); ++index) 
    		str[index] = toupper(str[index]); // capitalize the current character
	cout << str << endl;

	str = orig; // restore str to its original value

	// 2. for loop with iterators instead of subscripts
	for (auto it = str.begin(); it != str.end() && !isspace(*it); ++it)
		*it = toupper(*it); // capitalize the current character
	cout << str << endl;

	str = orig; // restore str to its original value

	// 3. while instead of a for with subscripts
	decltype(str.size()) index = 0; // subscript to look at characters in str
	while (index != str.size() && !isspace(str[index])) {
		str[index] = toupper(str[index]);  // capitalize the current character
		++index;        // advance the index to get the next character
	}
	cout << str << endl;

	// 4. while loop with iterators
	auto beg = str.begin();
	while (beg != str.end() && !isspace(*beg)) {
		*beg = toupper(*beg);
		++beg;
	}
	cout << str << endl;

	str = orig; // restore str to its original value

	// range for loop to process every character
	// first a loop to print the characters in str one character to a line
	for (auto c : str)      // for every char in str
		cout << c << endl;  // print the current character followed by a newline

	// next change every character in str
	for (auto &c : str)  // note: c is a reference 
		c = '*';         // assign a new value to the underlying char in str
	cout << str << endl;
	
	str = orig;  // restore str to its original value

	// equivalent code using traditional for loops
	// first print the characters in str
	for (decltype(str.size()) ix = 0; ix != str.size(); ++ix)
		cout << str[ix] << endl;  // print current character followd by a newline

	// next change every character in str
	for (decltype(str.size()) ix = 0; ix != str.size(); ++ix)
		str[ix] = '*';  // assigns a new value to the character in str
	cout << str << endl;

	str = orig;  // restore str to its original value

	// equivalent code using traditional for loops and iterators
	// first print the characters in str
	for (auto beg = str.begin(); beg != str.end(); ++beg)
		cout << *beg << endl;  // print current character followd by a newline

	// next change every character in str
	for (auto beg = str.begin(); beg != str.end(); ++beg)
		*beg = '*';  // assigns a new value to the character in str
	cout << str << endl;
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0xd88(%rip), %rsi       # 0x2004
leaq 0xd8c(%rip), %rdx       # 0x200f
leaq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x19d8
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq -0x70(%rbp), %rdi
callq 0x193a
cmpq $0x0, -0x48(%rbp)
je 0x1313
movq -0x50(%rbp), %rax
movb (%rax), %al
leaq -0x29(%rbp), %rsi
movb %al, (%rsi)
movq 0x2d14(%rip), %rdi      # 0x3fd8
movl $0x1, %edx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
cmpq $0x0, -0x48(%rbp)
je 0x1313
movq -0x50(%rbp), %rbx
movsbl (%rbx), %edi
callq 0x1110
movb %al, (%rbx)
movq -0x48(%rbp), %rdx
jmp 0x1315
xorl %edx, %edx
movq -0x50(%rbp), %rsi
movq 0x2cb8(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
cmpq $0x0, -0x48(%rbp)
je 0x1377
movq -0x50(%rbp), %rbx
movsbl (%rbx), %edi
callq 0x1110
movb %al, (%rbx)
movq -0x48(%rbp), %rdx
jmp 0x1379
xorl %edx, %edx
movq -0x50(%rbp), %rsi
movq 0x2c54(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x48(%rbp), %rbx
testq %rbx, %rbx
je 0x141a
movq -0x50(%rbp), %r15
movsbl (%r15), %r14d
movl %r14d, %edi
callq 0x1040
testl %eax, %eax
jne 0x1421
movl $0x1, %r12d
xorl %r13d, %r13d
movl %r14d, %edi
callq 0x1110
movb %al, (%r15)
movq -0x48(%rbp), %rbx
cmpq %rbx, %r12
je 0x141e
incq %r13
movq -0x50(%rbp), %r15
movsbl (%r15,%r12), %r14d
addq %r13, %r15
movl %r14d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x13e6
jmp 0x1421
xorl %ebx, %ebx
jmp 0x1421
movq %r12, %rbx
movq -0x50(%rbp), %rsi
movq 0x2bac(%rip), %rdi      # 0x3fd8
movq %rbx, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
testq %r14, %r14
je 0x14c2
movsbl (%rbx), %r15d
movl %r15d, %edi
callq 0x1040
testl %eax, %eax
jne 0x14c5
incq %rbx
movq %rbx, %r12
movl %r15d, %edi
callq 0x1110
movb %al, -0x1(%r12)
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
leaq (%rbx,%r14), %rax
cmpq %rax, %r12
je 0x14c5
movsbl (%r12), %r15d
movl %r15d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x148e
jmp 0x14c5
xorl %r14d, %r14d
movq 0x2b0c(%rip), %rdi      # 0x3fd8
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x48(%rbp), %rbx
testq %rbx, %rbx
je 0x1568
movq -0x50(%rbp), %r15
movsbl (%r15), %r14d
movl %r14d, %edi
callq 0x1040
testl %eax, %eax
jne 0x156f
movl $0x1, %r12d
xorl %r13d, %r13d
movl %r14d, %edi
callq 0x1110
movb %al, (%r15)
movq -0x48(%rbp), %rbx
cmpq %rbx, %r12
je 0x156c
incq %r13
movq -0x50(%rbp), %r15
movsbl (%r15,%r12), %r14d
addq %r13, %r15
movl %r14d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x1534
jmp 0x156f
xorl %ebx, %ebx
jmp 0x156f
movq %r12, %rbx
movq -0x50(%rbp), %rsi
movq 0x2a5e(%rip), %rdi      # 0x3fd8
movq %rbx, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
testq %r14, %r14
je 0x1603
movsbl (%rbx), %r15d
movl %r15d, %edi
callq 0x1040
testl %eax, %eax
jne 0x1606
incq %rbx
movq %rbx, %r12
movl %r15d, %edi
callq 0x1110
movb %al, -0x1(%r12)
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r14
leaq (%rbx,%r14), %rax
cmpq %rax, %r12
je 0x1606
movsbl (%r12), %r15d
movl %r15d, %edi
callq 0x1040
incq %r12
testl %eax, %eax
je 0x15cf
jmp 0x1606
xorl %r14d, %r14d
movq 0x29cb(%rip), %rdi      # 0x3fd8
movq %rbx, %rsi
movq %r14, %rdx
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %r13
testq %r13, %r13
je 0x16d4
xorl %r14d, %r14d
leaq -0x29(%rbp), %r15
movb (%rbx,%r14), %al
movb %al, -0x29(%rbp)
movl $0x1, %edx
movq 0x2962(%rip), %rdi      # 0x3fd8
movq %r15, %rsi
callq 0x10d0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
incq %r14
cmpq %r14, %r13
jne 0x1663
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
je 0x16d4
movq %rbx, %rdi
movl $0x2a, %esi
callq 0x1060
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %rdx
jmp 0x16d6
xorl %edx, %edx
movq 0x28fb(%rip), %rdi      # 0x3fd8
movq %rbx, %rsi
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
cmpq $0x0, -0x48(%rbp)
je 0x1799
xorl %r12d, %r12d
movq 0x28ab(%rip), %rbx      # 0x3fd8
leaq -0x29(%rbp), %r14
movq -0x50(%rbp), %rax
movb (%rax,%r12), %al
movb %al, -0x29(%rbp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x10d0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
incq %r12
cmpq -0x48(%rbp), %r12
jne 0x1731
testq %r12, %r12
je 0x1799
xorl %edx, %edx
movq -0x50(%rbp), %rax
movb $0x2a, (%rax,%rdx)
incq %rdx
cmpq -0x48(%rbp), %rdx
jne 0x1786
jmp 0x179b
xorl %edx, %edx
movq -0x50(%rbp), %rsi
movq 0x2832(%rip), %rdi      # 0x3fd8
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq -0x50(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1050
movq -0x50(%rbp), %rbx
cmpq $0x0, -0x48(%rbp)
je 0x1869
movq 0x27e4(%rip), %r14      # 0x3fd8
leaq -0x29(%rbp), %r15
movb (%rbx), %al
movb %al, -0x29(%rbp)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x10d0
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %r12, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
incq %rbx
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
leaq (%rax,%rcx), %rdx
cmpq %rdx, %rbx
jne 0x17f8
testq %rcx, %rcx
je 0x186d
movb $0x2a, (%rax)
incq %rax
movq -0x50(%rbp), %rbx
movq -0x48(%rbp), %rdx
leaq (%rbx,%rdx), %rcx
cmpq %rcx, %rax
jne 0x1850
jmp 0x1872
xorl %edx, %edx
jmp 0x1872
xorl %edx, %edx
movq %rax, %rbx
movq 0x275f(%rip), %rdi      # 0x3fd8
movq %rbx, %rsi
callq 0x10d0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x18c4
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x10c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x18dd
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10c0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1919
jmp 0x18fd
jmp 0x18fd
jmp 0x18fd
jmp 0x18fd
jmp 0x18fd
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x1919
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x10c0
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x1932
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x10c0
movq %rbx, %rdi
callq 0x10f0