From d38f3a79838220a666f80c7c2495a184d319a960 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Fri, 3 May 2013 19:35:47 -0700 Subject: Fix up line number handling to be unsigned int safe --- .../src/main/java/org/jf/util/IndentingWriter.java | 30 +++++++++++++ .../test/java/org/jf/util/IndentingWriterTest.java | 51 ++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 util/src/test/java/org/jf/util/IndentingWriterTest.java (limited to 'util') diff --git a/util/src/main/java/org/jf/util/IndentingWriter.java b/util/src/main/java/org/jf/util/IndentingWriter.java index 517b4b9d..95d6c320 100644 --- a/util/src/main/java/org/jf/util/IndentingWriter.java +++ b/util/src/main/java/org/jf/util/IndentingWriter.java @@ -192,6 +192,26 @@ public class IndentingWriter extends Writer { writeLine(buffer, bufferIndex, 24-bufferIndex); } + public void printSignedLongAsDec(long value) throws IOException { + int bufferIndex = 23; + + if (value < 0) { + value *= -1; + write('-'); + } + + do { + long digit = value % 10; + buffer[bufferIndex--] = (char)(digit + '0'); + + value = value / 10; + } while (value != 0); + + bufferIndex++; + + writeLine(buffer, bufferIndex, 24-bufferIndex); + } + public void printSignedIntAsDec(int value) throws IOException { int bufferIndex = 15; @@ -211,4 +231,14 @@ public class IndentingWriter extends Writer { writeLine(buffer, bufferIndex, 16-bufferIndex); } + + public void printUnsignedIntAsDec(int value) throws IOException { + int bufferIndex = 15; + + if (value < 0) { + printSignedLongAsDec(value & 0xFFFFFFFFL); + } else { + printSignedIntAsDec(value); + } + } } diff --git a/util/src/test/java/org/jf/util/IndentingWriterTest.java b/util/src/test/java/org/jf/util/IndentingWriterTest.java new file mode 100644 index 00000000..457262b5 --- /dev/null +++ b/util/src/test/java/org/jf/util/IndentingWriterTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.jf.util; + +import junit.framework.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.io.StringWriter; + +public class IndentingWriterTest { + @Test + public void testPrintSignedLongAsDec() throws IOException { + StringWriter stringWriter = new StringWriter(); + IndentingWriter writer = new IndentingWriter(stringWriter); + + writer.printUnsignedIntAsDec(-1); + writer.close(); + + Assert.assertEquals("4294967295", stringWriter.toString()); + } +} -- cgit v1.2.3