Convert FindMatch to an extension method. 'Hide' increment parameter
authorMichael Welch <michaelgwelch@gmail.com>
Wed, 14 Mar 2012 21:08:38 +0000 (16:08 -0500)
committerMichael Welch <michaelgwelch@gmail.com>
Wed, 14 Mar 2012 21:08:38 +0000 (16:08 -0500)
csharp/BrainmessShort/BrainmessShort.csproj
csharp/BrainmessShort/Main.cs
csharp/BrainmessShort/StringExtensions.cs [new file with mode: 0644]

index 41e0eef..7907c46 100644 (file)
@@ -62,6 +62,7 @@
   <ItemGroup>
     <Compile Include="Main.cs" />
     <Compile Include="MainTest.cs" />
+    <Compile Include="StringExtensions.cs" />
   </ItemGroup>
   <ItemGroup>
     <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
index f963eb6..b1874c5 100644 (file)
@@ -44,52 +44,17 @@ namespace BrainmessShort
                 case '[':
                     if (tape[tc] == 0)
                     {
-                        pc = JumpForward(program, pc);
+                        pc = program.FindMatch(pc - 1) + 1;
                     }
                         break;
                 case ']':
                     if (tape[tc] != 0)
                     {
-                        pc = JumpBackward(program, pc);
+                        pc = program.FindMatch(pc - 1);
                     }
                     break;
                 }
             }
         }
-
-       private static int JumpForward(string program, int pc)
-       {
-           const int increment = 1;
-           return FindMatch(program, pc - 1, increment) + 1;
-       }
-
-       private static int JumpBackward(string program, int pc)
-       {
-           const int increment = -1;
-           return FindMatch(program, pc - 1, increment);
-       }
-
-       /// <summary>
-       /// Finds the match for the bracket pointed to by
-       /// pc in the program. Increment tells the algorithm
-       /// which way to search.
-       /// </summary>
-       /// <param name="program"></param>
-       /// <param name="pc"></param>
-       /// <param name="increment"></param>
-       /// <returns></returns>
-       private static int FindMatch(string program, int pc, int increment)
-       {
-           int nestLevel = 1;
-           pc += increment;
-           while (nestLevel > 0)
-           {
-               char instruction = program[pc];
-               if (instruction == '[') nestLevel += increment;
-               else if (instruction == ']') nestLevel -= increment;
-               pc += increment;
-           }
-           return pc - increment;
-       }
    }
 }
diff --git a/csharp/BrainmessShort/StringExtensions.cs b/csharp/BrainmessShort/StringExtensions.cs
new file mode 100644 (file)
index 0000000..d9d9bbf
--- /dev/null
@@ -0,0 +1,38 @@
+using System;
+
+namespace BrainmessShort
+{
+    public static class StringExtensions
+    {
+        /// <summary>
+        /// Finds the match for the bracket pointed to by
+        /// pc in the program. Increment tells the algorithm
+        /// which way to search.
+        /// </summary>
+        /// <param name="program"></param>
+        /// <param name="pc"></param>
+        /// <param name="increment"></param>
+        /// <returns></returns>
+        private static int FindMatch(this string program, int pc, int increment)
+        {
+            int nestLevel = 1;
+            pc += increment;
+            while (nestLevel > 0)
+            {
+                char instruction = program[pc];
+                if (instruction == '[') nestLevel += increment;
+                else if (instruction == ']') nestLevel -= increment;
+                pc += increment;
+            }
+            return pc - increment;
+        }
+
+        public static int FindMatch(this string program, int pc)
+        {
+            if (program[pc] == '[') return program.FindMatch(pc, 1);
+            if (program[pc] == ']') return program.FindMatch(pc, -1);
+            throw new ArgumentException("The character at specified location is not a square bracket");
+
+        }
+    }
+}
\ No newline at end of file