Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 9th, 2012  |  syntax: Objective C  |  size: 1.65 KB  |  views: 113  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NSString* replace_substring(NSString* source, NSRange range, NSString* replacementString, int PaddingLength)
  2. {
  3.     NSLog(@"Before padding: %@", source);
  4.     auto SourceLength = [source length];
  5.     auto BeginIndex = range.location;
  6.     auto EndIndex = range.location + range.length;
  7.     cout << "Begin index: " << BeginIndex << " , End index: " << EndIndex << " Source length: " << SourceLength;
  8.     auto PreReplacementLength = EndIndex - BeginIndex;
  9.     auto ReplacementLength = [replacementString length];
  10.     auto PostReplacementLength = SourceLength + PaddingLength;
  11.     auto target = [[[NSString alloc] init] autorelease];
  12.     auto NextIndex = 0;
  13.     for(auto i = 0; i < BeginIndex; ++i)
  14.     {
  15.         auto range = [source rangeOfComposedCharacterSequenceAtIndex:NextIndex];
  16.         NextIndex += range.length;
  17.         auto appendPart = [source substringWithRange:range];
  18.         target = [target stringByAppendingString:appendPart];
  19.     }
  20.     for(auto i = 0; i < PreReplacementLength; ++i)
  21.     {
  22.         auto range = [source rangeOfComposedCharacterSequenceAtIndex:NextIndex];
  23.         NextIndex += range.length;
  24.     }
  25.     target = [target stringByAppendingString:replacementString];
  26.     int RemainingCharacters = ext::int64(SourceLength) - ext::int64(EndIndex) - 1;
  27.     cout << " Rem char: " << RemainingCharacters << endl;
  28.     for(auto i = 0; i < RemainingCharacters; ++i)
  29.     {
  30.         auto range = [source rangeOfComposedCharacterSequenceAtIndex:NextIndex];
  31.         NextIndex += range.length;
  32.         auto appendPart = [source substringWithRange:range];
  33.         target = [target stringByAppendingString:appendPart];
  34.     }
  35.     return target;
  36. }
clone this paste RAW Paste Data