-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRFind.cpp
86 lines (73 loc) · 2.3 KB
/
RFind.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* \file RFind.cpp
* \brief Find last occurrence of content in string
*
* \see https://cplusplus.com/reference/string/string/rfind/
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
void trace(
const std::string &logTitle, ///<
const std::string &strOrig, ///<
std::string &str, ///< [out]
const std::string &key, ///<
const std::size_t pos ///<
)
{
std::cout
<< "// " << logTitle << ": " << "\n"
<< "key '" << key << "' at " << STD_TRACE_VAR(pos) << std::endl;
if (pos == std::string::npos) {
std::cout << STD_TRACE_VAR(key) << " - Not found" << std::endl;
return;
}
str.erase(pos, std::string::npos);
std::cout
<< "Erase:\n"
<< STD_TRACE_VAR(strOrig) << " -> "
<< STD_TRACE_VAR(str) << ", "<< STD_TRACE_VAR(str.size()) << "\n"
<< std::endl;
}
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
const std::string strOrig = "aaa|bbb|ccc|ddd|eee";
const std::size_t sizeMax = 15;
std::string str = strOrig;
const std::string key = "|";
// BookedAvailGdsHotelRoom.xml
{
const std::size_t pos = str.rfind(key);
::trace("str.rfind(key)", strOrig, str, key, pos);
}
// BookedAvailGdsHotelRoom.xml
{
const std::size_t pos = str.rfind(key, sizeMax);
::trace("str.rfind(key, sizeMax)", strOrig, str, key, pos);
}
// AvailAffiliateHotelRoom.xml
{
const std::size_t pos = str.find(key, sizeMax);
::trace("str.find(key, sizeMax)", strOrig, str, key, pos);
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
/**
* // str.rfind(key):
* key '|' at pos: 15
* Erase:
* strOrig: aaa|bbb|ccc|ddd|eee -> str: aaa|bbb|ccc|ddd, str.size(): 15
*
* // str.rfind(key, sizeMax):
* key '|' at pos: 15
* Erase:
* strOrig: aaa|bbb|ccc|ddd|eee -> str: aaa|bbb|ccc|ddd, str.size(): 15
*
* // str.find(key, sizeMax):
* key '|' at pos: 15
* Erase:
* strOrig: aaa|bbb|ccc|ddd|eee -> str: aaa|bbb|ccc|ddd, str.size(): 15
*/