MCPcopy Create free account
hub / github.com/KDE/kdiff3 / safeStringJoin

Function safeStringJoin

src/common.cpp:70–97  ·  view source on GitHub ↗

safeStringJoin and safeStringSplit allow to convert a stringlist into a string and back safely, even if the individual strings in the list contain the separator character.

Source from the content-addressed store, hash-verified

68// safeStringJoin and safeStringSplit allow to convert a stringlist into a string and back
69// safely, even if the individual strings in the list contain the separator character.
70QString safeStringJoin(const QStringList& sl, char sepChar, char metaChar)
71{
72 // Join the strings in the list, using the separator ','
73 // If a string contains the separator character, it will be replaced with "\,".
74 // Any occurrences of "\" (one backslash) will be replaced with "\\" (2 backslashes)
75
76 assert(sepChar != metaChar);
77
78 QString sep;
79 sep += sepChar;
80 QString meta;
81 meta += metaChar;
82
83 QString safeString;
84
85 QStringList::const_iterator i;
86 for(i = sl.begin(); i != sl.end(); ++i)
87 {
88 QString s = *i;
89 s.replace(meta, meta + meta); // "\" -> "\\"
90 s.replace(sep, meta + sep); // "," -> "\,"
91 if(i == sl.begin())
92 safeString = s;
93 else
94 safeString += sep + s;
95 }
96 return safeString;
97}
98
99// Split a string that was joined with safeStringJoin
100QStringList safeStringSplit(const QString& s, char sepChar, char metaChar)

Callers 1

writeEntryMethod · 0.85

Calls 1

endMethod · 0.80

Tested by

no test coverage detected