Ejemplo n.º 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void test() throws Exception
	  public virtual void test()
	  {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int ITERS = atLeast(1000);
		int ITERS = atLeast(1000);

		RollingCharBuffer buffer = new RollingCharBuffer();

		Random random = random();
		for (int iter = 0;iter < ITERS;iter++)
		{
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int stringLen = random.nextBoolean() ? random.nextInt(50) : random.nextInt(20000);
		  int stringLen = random.nextBoolean() ? random.Next(50) : random.Next(20000);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String s;
		  string s;
		  if (stringLen == 0)
		  {
			s = "";
		  }
		  else
		  {
			s = TestUtil.randomUnicodeString(random, stringLen);
		  }
		  if (VERBOSE)
		  {
			Console.WriteLine("\nTEST: iter=" + iter + " s.length()=" + s.Length);
		  }
		  buffer.reset(new StringReader(s));
		  int nextRead = 0;
		  int availCount = 0;
		  while (nextRead < s.Length)
		  {
			if (VERBOSE)
			{
			  Console.WriteLine("  cycle nextRead=" + nextRead + " avail=" + availCount);
			}
			if (availCount == 0 || random.nextBoolean())
			{
			  // Read next char
			  if (VERBOSE)
			  {
				Console.WriteLine("    new char");
			  }
			  assertEquals(s[nextRead], buffer.get(nextRead));
			  nextRead++;
			  availCount++;
			}
			else if (random.nextBoolean())
			{
			  // Read previous char
			  int pos = TestUtil.Next(random, nextRead - availCount, nextRead - 1);
			  if (VERBOSE)
			  {
				Console.WriteLine("    old char pos=" + pos);
			  }
			  assertEquals(s[pos], buffer.get(pos));
			}
			else
			{
			  // Read slice
			  int length;
			  if (availCount == 1)
			  {
				length = 1;
			  }
			  else
			  {
				length = TestUtil.Next(random, 1, availCount);
			  }
			  int start;
			  if (length == availCount)
			  {
				start = nextRead - availCount;
			  }
			  else
			  {
				start = nextRead - availCount + random.Next(availCount - length);
			  }
			  if (VERBOSE)
			  {
				Console.WriteLine("    slice start=" + start + " length=" + length);
			  }
			  assertEquals(s.Substring(start, length), new string(buffer.get(start, length)));
			}

			if (availCount > 0 && random.Next(20) == 17)
			{
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int toFree = random.nextInt(availCount);
			  int toFree = random.Next(availCount);
			  if (VERBOSE)
			  {
				Console.WriteLine("    free " + toFree + " (avail=" + (availCount - toFree) + ")");
			  }
			  buffer.freeBefore(nextRead - (availCount - toFree));
			  availCount -= toFree;
			}
		  }
		}
	  }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void test() throws Exception
        public virtual void test()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int ITERS = atLeast(1000);
            int ITERS = atLeast(1000);

            RollingCharBuffer buffer = new RollingCharBuffer();

            Random random = random();

            for (int iter = 0; iter < ITERS; iter++)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int stringLen = random.nextBoolean() ? random.nextInt(50) : random.nextInt(20000);
                int stringLen = random.nextBoolean() ? random.Next(50) : random.Next(20000);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String s;
                string s;
                if (stringLen == 0)
                {
                    s = "";
                }
                else
                {
                    s = TestUtil.randomUnicodeString(random, stringLen);
                }
                if (VERBOSE)
                {
                    Console.WriteLine("\nTEST: iter=" + iter + " s.length()=" + s.Length);
                }
                buffer.reset(new StringReader(s));
                int nextRead   = 0;
                int availCount = 0;
                while (nextRead < s.Length)
                {
                    if (VERBOSE)
                    {
                        Console.WriteLine("  cycle nextRead=" + nextRead + " avail=" + availCount);
                    }
                    if (availCount == 0 || random.nextBoolean())
                    {
                        // Read next char
                        if (VERBOSE)
                        {
                            Console.WriteLine("    new char");
                        }
                        assertEquals(s[nextRead], buffer.get(nextRead));
                        nextRead++;
                        availCount++;
                    }
                    else if (random.nextBoolean())
                    {
                        // Read previous char
                        int pos = TestUtil.Next(random, nextRead - availCount, nextRead - 1);
                        if (VERBOSE)
                        {
                            Console.WriteLine("    old char pos=" + pos);
                        }
                        assertEquals(s[pos], buffer.get(pos));
                    }
                    else
                    {
                        // Read slice
                        int length;
                        if (availCount == 1)
                        {
                            length = 1;
                        }
                        else
                        {
                            length = TestUtil.Next(random, 1, availCount);
                        }
                        int start;
                        if (length == availCount)
                        {
                            start = nextRead - availCount;
                        }
                        else
                        {
                            start = nextRead - availCount + random.Next(availCount - length);
                        }
                        if (VERBOSE)
                        {
                            Console.WriteLine("    slice start=" + start + " length=" + length);
                        }
                        assertEquals(s.Substring(start, length), new string(buffer.get(start, length)));
                    }

                    if (availCount > 0 && random.Next(20) == 17)
                    {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int toFree = random.nextInt(availCount);
                        int toFree = random.Next(availCount);
                        if (VERBOSE)
                        {
                            Console.WriteLine("    free " + toFree + " (avail=" + (availCount - toFree) + ")");
                        }
                        buffer.freeBefore(nextRead - (availCount - toFree));
                        availCount -= toFree;
                    }
                }
            }
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public int read() throws java.io.IOException
        public override int read()
        {
            //System.out.println("\nread");
            while (true)
            {
                if (replacement != null && replacementPointer < replacement.length)
                {
                    //System.out.println("  return repl[" + replacementPointer + "]=" + replacement.chars[replacement.offset + replacementPointer]);
                    return(replacement.chars[replacement.offset + replacementPointer++]);
                }

                // TODO: a more efficient approach would be Aho/Corasick's
                // algorithm
                // (http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm)
                // or this generalizatio: www.cis.uni-muenchen.de/people/Schulz/Pub/dictle5.ps
                //
                // I think this would be (almost?) equivalent to 1) adding
                // epsilon arcs from all final nodes back to the init
                // node in the FST, 2) adding a .* (skip any char)
                // loop on the initial node, and 3) determinizing
                // that.  Then we would not have to restart matching
                // at each position.

                int      lastMatchLen = -1;
                CharsRef lastMatch    = null;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int firstCH = buffer.get(inputOff);
                int firstCH = buffer.get(inputOff);
                if (firstCH != -1)
                {
                    FST.Arc <CharsRef> arc = cachedRootArcs[Convert.ToChar((char)firstCH)];
                    if (arc != null)
                    {
                        if (!FST.targetHasArcs(arc))
                        {
                            // Fast pass for single character match:
                            Debug.Assert(arc.Final);
                            lastMatchLen = 1;
                            lastMatch    = arc.output;
                        }
                        else
                        {
                            int      lookahead = 0;
                            CharsRef output    = arc.output;
                            while (true)
                            {
                                lookahead++;

                                if (arc.Final)
                                {
                                    // Match! (to node is final)
                                    lastMatchLen = lookahead;
                                    lastMatch    = outputs.add(output, arc.nextFinalOutput);
                                    // Greedy: keep searching to see if there's a
                                    // longer match...
                                }

                                if (!FST.targetHasArcs(arc))
                                {
                                    break;
                                }

                                int ch = buffer.get(inputOff + lookahead);
                                if (ch == -1)
                                {
                                    break;
                                }
                                if ((arc = map.findTargetArc(ch, arc, scratchArc, fstReader)) == null)
                                {
                                    // Dead end
                                    break;
                                }
                                output = outputs.add(output, arc.output);
                            }
                        }
                    }
                }

                if (lastMatch != null)
                {
                    inputOff += lastMatchLen;
                    //System.out.println("  match!  len=" + lastMatchLen + " repl=" + lastMatch);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int diff = lastMatchLen - lastMatch.length;
                    int diff = lastMatchLen - lastMatch.length;

                    if (diff != 0)
                    {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int prevCumulativeDiff = getLastCumulativeDiff();
                        int prevCumulativeDiff = LastCumulativeDiff;
                        if (diff > 0)
                        {
                            // Replacement is shorter than matched input:
                            addOffCorrectMap(inputOff - diff - prevCumulativeDiff, prevCumulativeDiff + diff);
                        }
                        else
                        {
                            // Replacement is longer than matched input: remap
                            // the "extra" chars all back to the same input
                            // offset:
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int outputStart = inputOff - prevCumulativeDiff;
                            int outputStart = inputOff - prevCumulativeDiff;
                            for (int extraIDX = 0; extraIDX < -diff; extraIDX++)
                            {
                                addOffCorrectMap(outputStart + extraIDX, prevCumulativeDiff - extraIDX - 1);
                            }
                        }
                    }

                    replacement        = lastMatch;
                    replacementPointer = 0;
                }
                else
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int ret = buffer.get(inputOff);
                    int ret = buffer.get(inputOff);
                    if (ret != -1)
                    {
                        inputOff++;
                        buffer.freeBefore(inputOff);
                    }
                    return(ret);
                }
            }
        }