Advertisement
brunobozic

Microoptimizations

Sep 15th, 2023
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         string sentence = "Hello how are you?";
  10.         string targetWord = "how";
  11.         string replaceWith = "what is";
  12.         string indent = "  ";
  13.  
  14.         // Calculate lengths
  15.         int originalLength = sentence.Length;
  16.         int targetLength = targetWord.Length;
  17.         int replacementLength = replaceWith.Length;
  18.         int indentLength = indent.Length;
  19.  
  20.         // Calculate the length for the new sentence
  21.         int newLength = originalLength - targetLength + replacementLength + (indentLength * 6); // 6 indentations
  22.  
  23.         Span<char> originalSpan = stackalloc char[originalLength];
  24.         Span<char> newSpan = stackalloc char[newLength];
  25.  
  26.         // Copy original sentence and target word to spans
  27.         sentence.AsSpan().CopyTo(originalSpan);
  28.         targetWord.AsSpan().CopyTo(stackalloc char[targetLength]);
  29.  
  30.         int newSpanIndex = 0;
  31.  
  32.         // Add initial indentation
  33.         CopyAndAdvance(ref newSpan, indent.AsSpan(), ref newSpanIndex);
  34.  
  35.         // Find and replace target word, add indentations
  36.         for (int i = 0; i < originalSpan.Length; i++)
  37.         {
  38.             if (IsWordMatch(originalSpan.Slice(i, originalSpan.Length - i), targetWord.AsSpan()))
  39.             {
  40.                 CopyAndAdvance(ref newSpan, replaceWith.AsSpan(), ref newSpanIndex);
  41.                 i += targetLength - 1; // Skip the target word in original sentence
  42.             }
  43.             else
  44.             {
  45.                 newSpan[newSpanIndex++] = originalSpan[i];
  46.             }
  47.  
  48.             // Add indentation after each word or at the end
  49.             if (i == originalSpan.Length - 1 || originalSpan[i] == ' ')
  50.             {
  51.                 CopyAndAdvance(ref newSpan, indent.AsSpan(), ref newSpanIndex);
  52.             }
  53.         }
  54.  
  55.         // Convert result back to string
  56.         string result = new string(newSpan);
  57.         Console.WriteLine($"Original: {sentence}");
  58.         Console.WriteLine($"Modified: {result}");
  59.     }
  60.  
  61.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  62.     static void CopyAndAdvance(ref Span<char> destination, ReadOnlySpan<char> source, ref int index)
  63.     {
  64.         source.CopyTo(destination.Slice(index));
  65.         index += source.Length;
  66.     }
  67.  
  68.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69.     static bool IsWordMatch(ReadOnlySpan<char> sentence, ReadOnlySpan<char> word)
  70.     {
  71.         return sentence.Length >= word.Length && sentence.Slice(0, word.Length).SequenceEqual(word);
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement