GenuineSounds

BrainFuck Encode/Decode

Dec 7th, 2014
679
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package com.genuineminecraft.bf;
  2.  
  3. public class Test {
  4.  
  5.     public static String test = "+++++++++[->+++++++++<]>+++.<++++[->++++<]>+.++++++++++++++.+.-----------.+++++.-------.<++++++++[->--------<]>-------.<+++++++++[->+++++++++<]>+++.---------------.++++++++++++++.+.-----------.+++++.-------.<++++++++[->--------<]>-------.<++++[->++++<]>+.<++++[->----<]>-.<++++[->++++<]>++.";
  6.  
  7.     public static void main(String[] args) {
  8.         timeTrials();
  9.         qualityCheck();
  10.     }
  11.  
  12.     public static void qualityCheck() {
  13.         String min = encode(test);
  14.         String mag = decode(min);
  15.         System.out.println("Debug: " + test);
  16.         System.out.println("Debug: " + min);
  17.         System.out.println("Debug: " + mag);
  18.         if (!test.equals(mag))
  19.             System.out.println("Error: Test Failed");
  20.         else
  21.             System.out.println("Success: Test Passed");
  22.     }
  23.  
  24.     public static void timeTrials() {
  25.         long start, stop;
  26.         int count = 1000000;
  27.         System.out.println("Debug: Running " + count + " times.");
  28.         start = System.currentTimeMillis();
  29.         for (int i = 0; i < count; i++)
  30.             encode_1(test);
  31.         stop = System.currentTimeMillis();
  32.         System.out.println("Debug 1: " + (stop - start) + " millseconds.");
  33.         start = System.currentTimeMillis();
  34.         for (int i = 0; i < count; i++)
  35.             encode_2(test);
  36.         stop = System.currentTimeMillis();
  37.         System.out.println("Debug 2: " + (stop - start) + " millseconds.");
  38.         start = System.currentTimeMillis();
  39.         for (int i = 0; i < count; i++)
  40.             encode_3(test);
  41.         stop = System.currentTimeMillis();
  42.         System.out.println("Debug 3: " + (stop - start) + " millseconds.");
  43.     }
  44.  
  45.     public static String encode(String in) {
  46.         return encode_1(in);
  47.     }
  48.  
  49.     public static String decode(String in) {
  50.         return decode_1(in);
  51.     }
  52.  
  53.     private static String encode_1(String in) {
  54.         String out = "";
  55.         int counter = 0;
  56.         for (int place = 0; place < in.length(); place++) {
  57.             char compare = in.charAt(place);
  58.             if (in.length() > place + 1 && compare == in.charAt(place + 1)) {
  59.                 counter++;
  60.                 continue;
  61.             }
  62.             switch (counter) {
  63.                 case 0:
  64.                     out += Character.toString(compare);
  65.                     break;
  66.                 case 1:
  67.                     out += Character.toString(compare) + Character.toString(compare);
  68.                     break;
  69.                 default:
  70.                     out += Integer.toString(counter + 1) + Character.toString(compare);
  71.                     break;
  72.             }
  73.             counter = 0;
  74.         }
  75.         return out;
  76.     }
  77.  
  78.     private static String encode_2(String in) {
  79.         String out = "";
  80.         int counter = 0;
  81.         for (int place = 0; place < in.length(); place++) {
  82.             char compare = in.charAt(place);
  83.             if (in.length() > place + 1 && compare == in.charAt(place + 1)) {
  84.                 counter++;
  85.                 continue;
  86.             }
  87.             out += (counter == 0 ? "" :
  88.                 counter == 1 ? Character.toString(compare) : Integer.toString(counter + 1)) +
  89.                     Character.toString(compare);
  90.             counter = 0;
  91.         }
  92.         return out;
  93.     }
  94.  
  95.     private static String encode_3(String in) {
  96.         String out = "";
  97.         while (in.length() > 0) {
  98.             int counter = 0;
  99.             char compare = in.charAt(0);
  100.             while (counter < in.length() && in.charAt(counter) == compare)
  101.                 counter++;
  102.             in = in.substring(counter - (counter == 2 ? 1 : 0));
  103.             if (counter > 2)
  104.                 out += counter;
  105.             out += compare;
  106.         }
  107.         return out;
  108.     }
  109.  
  110.     private static String decode_1(String in) {
  111.         String out = "";
  112.         for (int i = 0; i < in.length(); i++) {
  113.             String t = "";
  114.             while (Character.isDigit(in.charAt(i)))
  115.                 t += in.charAt(i++);
  116.             int n = 1;
  117.             if (!t.isEmpty())
  118.                 n = Integer.parseInt(t, 10);
  119.             for (int j = 0; j < n; j++)
  120.                 out += in.charAt(i);
  121.         }
  122.         return out;
  123.     }
  124. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment