// File: /workspace/github_cpp_makefile_star2_O2/iZhangHui[P]CppPrimer/CH09_SequentialContainers/examples/str_assignOps.cc
// Function: main
int main()
{
    string s = "some string", s2 = "some other string";
    // equivalent ways to insert all the characters from s2 at beginning of s
    // insert iterator range before s.begin()
    s.insert(s.begin(), s2.begin(), s2.end());
    cout << "insert iterators version:        " << s << endl;

    s = "some string";
    s.insert(0, s2); // insert a copy of s2 before position 0 in s
    cout << "insert string at given position: " << s << endl;

    s = "some string";
    // insert s2.size() characters from s2 starting at s2[0] before s[0]
    s.insert(0, s2, 0, s2.size());
    cout << "insert positional version:       " << s << endl;


    // s = "";  // s is now empty
    s = "x";
    vector<char> c_vec(1, 'a');
    // insert characters from c_vec into s
    s.insert(s.begin(), c_vec.begin(), c_vec.end());
    s.insert(s.size(), 5, '!'); // add five exclamation points at the end of s
    cout << s << endl;

    s.erase(s.size() - 5, 5);   // erase the last five characters from s
    cout << s << endl;

    s = "";  // s is now empty
    const char *cp = "Stately, plump Buck";
    s.assign(cp, 7);            // s == "Stately"
    cout << s << endl;
    s.insert(s.size(), cp + 7); // s == "Stately, plump Buck"
    cout << s << endl;

    s = "C++ Primer";  // reset s and s2
    s2 = s;            // to "C++ Primer"
    s.insert(s.size(), " 4th Ed."); // s == "C++ Primer 4th Ed."
    s2.append(" 4th Ed."); // equivalent: appends " 4th Ed." to s2;
    cout << s << " " << s2 << endl;

    // two ways to replace "4th" by "5th"
    // 1. insert and erase
    s.erase(11, 3);                 // s == "C++ Primer Ed."
    s.insert(11, "5th");            // s == "C++ Primer 5th Ed."

    // 2. use replace
    // erase three characters starting at position 11
    //  and then insert "5th"
    s2.replace(11, 3, "5th"); // equivalent: s == s2

    cout << s << " " << s2 << endl;

    // two ways to replace "5th" by "Fifth"
    // 1. use replace if we know where the string we want to replace is
    s.replace(11, 3, "Fifth"); // s == "C++ Primer Fifth Ed."

    // 2. call find first to get position from which to replace
    auto pos = s2.find("5th");
    if (pos != string::npos)
        s2.replace(pos, 3, "Fifth");
    else
        cout << "something's wrong, s2 is: " << s2 << endl;
    cout << s << " " << s2 << endl;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x68, %rsp
leaq 0xcb8(%rip), %rsi       # 0x3004
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x2070
leaq 0xcb0(%rip), %rsi       # 0x3010
leaq -0x50(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x2070
leaq -0x30(%rbp), %rdi
movq (%rdi), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
addq %rdx, %rcx
callq 0x2714
movq 0x2c4d(%rip), %rdi      # 0x4fd8
leaq 0xc90(%rip), %rsi       # 0x3022
callq 0x2110
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq 0xc52(%rip), %rsi       # 0x3004
leaq -0x30(%rbp), %rdi
callq 0x21d0
leaq -0x30(%rbp), %rdi
leaq -0x50(%rbp), %rdx
xorl %esi, %esi
callq 0x2180
movq 0x2c07(%rip), %rdi      # 0x4fd8
leaq 0xc6c(%rip), %rsi       # 0x3044
callq 0x2110
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq 0xc0c(%rip), %rsi       # 0x3004
leaq -0x30(%rbp), %rdi
callq 0x21d0
leaq -0x50(%rbp), %rdx
movq 0x8(%rdx), %r8
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
xorl %ecx, %ecx
callq 0x2140
movq 0x2bbb(%rip), %rdi      # 0x4fd8
leaq 0xc42(%rip), %rsi       # 0x3066
callq 0x2110
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq 0xc44(%rip), %rsi       # 0x3088
leaq -0x30(%rbp), %rdi
callq 0x21d0
leaq -0xa(%rbp), %rdx
movb $0x61, (%rdx)
leaq -0x68(%rbp), %rdi
pushq $0x1
popq %rsi
leaq -0x9(%rbp), %rcx
callq 0x273e
leaq -0x30(%rbp), %rdi
movq (%rdi), %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rcx
callq 0x279c
leaq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
pushq $0x5
popq %rdx
pushq $0x21
popq %rcx
callq 0x21f0
movq 0x2b46(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
addq $-0x5, %rsi
pushq $0x5
popq %rdx
callq 0x2030
movq 0x2b1a(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq 0xb6d(%rip), %rsi       # 0x3043
leaq -0x30(%rbp), %rdi
callq 0x21d0
leaq 0xba4(%rip), %rsi       # 0x308a
leaq -0x30(%rbp), %rdi
pushq $0x7
popq %rdx
callq 0x2150
movq 0x2adf(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
leaq 0xb78(%rip), %rdx       # 0x3091
callq 0x2040
movq 0x2ab3(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq 0xb61(%rip), %rsi       # 0x309e
leaq -0x30(%rbp), %rdi
callq 0x21d0
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x20c0
leaq -0x30(%rbp), %rdi
movq 0x8(%rdi), %rsi
leaq 0xb47(%rip), %rdx       # 0x30a9
callq 0x2040
leaq 0xb3b(%rip), %rsi       # 0x30a9
leaq -0x50(%rbp), %rdi
callq 0x21c0
movq 0x2a5a(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
leaq 0xab4(%rip), %rsi       # 0x3042
movq %rax, %rdi
callq 0x2110
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq -0x30(%rbp), %rdi
pushq $0xb
popq %rsi
pushq $0x3
popq %rdx
callq 0x2030
leaq 0xaf2(%rip), %rdx       # 0x30b2
leaq -0x30(%rbp), %rdi
pushq $0xb
popq %rsi
callq 0x2040
leaq 0xadf(%rip), %rcx       # 0x30b2
leaq -0x50(%rbp), %rdi
pushq $0xb
popq %rsi
pushq $0x3
popq %rdx
callq 0x20e0
movq 0x29ef(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
leaq 0xa49(%rip), %rsi       # 0x3042
movq %rax, %rdi
callq 0x2110
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq 0xa9a(%rip), %rcx       # 0x30b6
leaq -0x30(%rbp), %rdi
pushq $0xb
popq %rsi
pushq $0x3
popq %rdx
callq 0x20e0
leaq 0xa80(%rip), %rsi       # 0x30b2
leaq -0x50(%rbp), %rdi
xorl %edx, %edx
callq 0x21e0
cmpq $-0x1, %rax
je 0x265b
leaq 0xa6c(%rip), %rcx       # 0x30b6
leaq -0x50(%rbp), %rdi
pushq $0x3
popq %rdx
movq %rax, %rsi
callq 0x20e0
jmp 0x2682
movq 0x2976(%rip), %rdi      # 0x4fd8
leaq 0xa53(%rip), %rsi       # 0x30bc
callq 0x2110
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
movq 0x294f(%rip), %rdi      # 0x4fd8
leaq -0x30(%rbp), %rsi
callq 0x2100
leaq 0x9a9(%rip), %rsi       # 0x3042
movq %rax, %rdi
callq 0x2110
leaq -0x50(%rbp), %rsi
movq %rax, %rdi
callq 0x2100
movq %rax, %rdi
callq 0x20a0
leaq -0x68(%rbp), %rdi
callq 0x27ee
leaq -0x50(%rbp), %rdi
callq 0x2090
leaq -0x30(%rbp), %rdi
callq 0x2090
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %rbp
retq
jmp 0x26ed
jmp 0x26e8
jmp 0x26e8
movq %rax, %rbx
jmp 0x2702
jmp 0x26ed
jmp 0x26ed
movq %rax, %rbx
jmp 0x26f9
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x27ee
leaq -0x50(%rbp), %rdi
callq 0x2090
leaq -0x30(%rbp), %rdi
callq 0x2090
movq %rbx, %rdi
callq 0x2190
nop