Created
December 17, 2010 23:55
-
-
Save tomgibara/745933 to your computer and use it in GitHub Desktop.
A small class for converting integers to their textual representation without creating any garbage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tomgibara.android.util; | |
/** | |
* <p> | |
* Writes ints into char array without generating any objects. This is typically | |
* useful in instances where numbers must be rendered within a game-loop or | |
* animation. Instances of this class are not safe for multithreaded use. | |
* </p> | |
* | |
* <p> | |
* Use of the class proceeds like this: | |
* </p> | |
* | |
* <pre> | |
* <code> | |
* final NumberWriter writer = new NumberWriter(); | |
* final char[] chars = writer.getChars(); | |
* final int length = chars.length; | |
* ... | |
* int n = 39088169; | |
* int offset = writer.write(n); | |
* canvas.drawText(chars, offset, length - offset, x, y, paint); | |
* ... | |
* </code> | |
* </pre> | |
* | |
* @author Tom Gibara | |
* | |
*/ | |
public class NumberWriter { | |
/** | |
* Big enough for a {@link Integer.MIN_VALUE}. | |
*/ | |
private static int CHARS_LEN = 11; | |
/** | |
* Stores the generated characters. | |
*/ | |
private final char[] mChars = new char[CHARS_LEN]; | |
/** | |
* The character array into which numbers will be written. One character | |
* array is used for the lifetime of this object. | |
* | |
* @return an array of characters | |
*/ | |
public char[] getChars() { | |
return mChars; | |
} | |
/** | |
* Writes the supplied integer to this object's character array. The | |
* characters in the integer's textual representation will begin at the | |
* returned index and continue until the end of the array. | |
* | |
* @param n | |
* any integer | |
* @return the index of the first character | |
*/ | |
/* | |
* Note that this method could be implemented more efficiently, but it is | |
* adequate for most purposes. | |
*/ | |
public int write(int n) { | |
final char[] cs = mChars; | |
final boolean negative; | |
if (n < 0) { | |
negative = true; | |
n = -n; | |
if (n < 0) { | |
// Integer.MIN_VALUE is a special case | |
cs[CHARS_LEN - 1] = '8'; | |
cs[CHARS_LEN - 2] = '4'; | |
cs[CHARS_LEN - 3] = '6'; | |
cs[CHARS_LEN - 4] = '3'; | |
cs[CHARS_LEN - 5] = '8'; | |
cs[CHARS_LEN - 6] = '4'; | |
cs[CHARS_LEN - 7] = '7'; | |
cs[CHARS_LEN - 8] = '4'; | |
cs[CHARS_LEN - 9] = '1'; | |
cs[CHARS_LEN - 10] = '2'; | |
cs[CHARS_LEN - 11] = '-'; | |
return CHARS_LEN - 11; | |
} | |
} else { | |
negative = false; | |
} | |
int i = CHARS_LEN; | |
while (n > 9) { | |
final int q = n / 10; | |
final int r = n - q * 10; | |
cs[--i] = (char)(48 + r); | |
n = q; | |
} | |
cs[--i] = (char)(48 + n); | |
if (negative) cs[--i] = '-'; | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment