NSString* replace_substring(NSString* source, NSRange range, NSString* replacementString, int PaddingLength)
{
NSLog(@"Before padding: %@", source);
auto SourceLength = [source length];
auto BeginIndex = range.location;
auto EndIndex = range.location + range.length;
cout << "Begin index: " << BeginIndex << " , End index: " << EndIndex << " Source length: " << SourceLength;
auto PreReplacementLength = EndIndex - BeginIndex;
auto ReplacementLength = [replacementString length];
auto PostReplacementLength = SourceLength + PaddingLength;
auto target = [[[NSString alloc] init] autorelease];
auto NextIndex = 0;
for(auto i = 0; i < BeginIndex; ++i)
{
auto range = [source rangeOfComposedCharacterSequenceAtIndex:NextIndex];
NextIndex += range.length;
auto appendPart = [source substringWithRange:range];
target = [target stringByAppendingString:appendPart];
}
for(auto i = 0; i < PreReplacementLength; ++i)
{
auto range = [source rangeOfComposedCharacterSequenceAtIndex:NextIndex];
NextIndex += range.length;
}
target = [target stringByAppendingString:replacementString];
int RemainingCharacters = ext::int64(SourceLength) - ext::int64(EndIndex) - 1;
cout << " Rem char: " << RemainingCharacters << endl;
for(auto i = 0; i < RemainingCharacters; ++i)
{
auto range = [source rangeOfComposedCharacterSequenceAtIndex:NextIndex];
NextIndex += range.length;
auto appendPart = [source substringWithRange:range];
target = [target stringByAppendingString:appendPart];
}
return target;
}