diff --git a/engine/String.cpp b/engine/String.cpp index 88e82f4f..3735c3cf 100644 --- a/engine/String.cpp +++ b/engine/String.cpp @@ -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) diff --git a/yateclass.h b/yateclass.h index 4fb4a89a..bd848656 100644 --- a/yateclass.h +++ b/yateclass.h @@ -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.