Added String::insert().

git-svn-id: http://yate.null.ro/svn/yate/trunk@6512 acf43c95-373e-0410-b603-e72c3f656dc1
This commit is contained in:
marian 2021-08-16 06:40:44 +00:00
parent a964723b52
commit d476e43a2a
2 changed files with 58 additions and 0 deletions

View File

@ -1219,6 +1219,42 @@ String& String::append(double value, unsigned int decimals)
return operator+=(buf);
}
String& String::insert(unsigned int pos, const char* value, int len)
{
if (!(value && *value && len))
return *this;
if (pos >= length())
return append(value,len);
if (len < 0)
len = ::strlen(value);
if (!len)
return *this;
int olen = length();
int sLen = len + olen;
char* tmp1 = m_string;
char* tmp2 = (char*)::malloc(sLen + 1);
if (!tmp2) {
Debug("String",DebugFail,"malloc(%d) returned NULL!",sLen + 1);
return *this;
}
if (!pos) {
::strncpy(tmp2,value,len);
::strncpy(tmp2 + len,m_string,olen);
}
else {
::strncpy(tmp2,m_string,pos);
::strncpy(tmp2 + pos,value,len);
::strncpy(tmp2 + pos + len,m_string + pos,olen - pos);
}
tmp2[sLen] = 0;
m_string = tmp2;
m_length = sLen;
::free(tmp1);
changed();
return *this;
}
static char* string_printf(unsigned int& length, const char* format, va_list& va)
{
if (TelEngine::null(format) || !length)

View File

@ -2827,6 +2827,28 @@ public:
*/
String& append(double value, unsigned int decimals = 3);
/**
* Insert a string into current string
* @param pos Position to insert. String will be appended if position is greater than curent length
* @param value String to insert
* @param len Length of the data to copy, -1 for full string
* @return Reference to the String
*/
String& insert(unsigned int pos, const char* value, int len = -1);
/**
* Insert a character into current string
* @param pos Position to insert. The character will be appended if position is greater than curent length
* @param value Character to insert. NUL character will be ignored
* @return Reference to the String
*/
inline String& insert(unsigned int pos, char value) {
if (!value)
return *this;
char s[] = {value};
return insert(pos,s,1);
}
/**
* Build a String in a printf style.
* @param format The output format.