Created
September 13, 2012 15:22
-
-
Save MobiDevelop/3715052 to your computer and use it in GitHub Desktop.
A zero-allocation way to append ints and longs to a StringBuilder in Android.
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
/******************************************************************************* | |
* Copyright 2012 Justin Shapcott | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
******************************************************************************/ | |
package nEx.Utilities; | |
public class Appender { | |
final static private long[] cutoffs = new long[] { | |
9L, | |
99L, | |
999L, | |
9999L, | |
99999L, | |
999999L, | |
9999999L, | |
99999999L, | |
999999999L, | |
9999999999L, | |
99999999999L, | |
999999999999L, | |
9999999999999L, | |
99999999999999L, | |
999999999999999L, | |
9999999999999999L, | |
99999999999999999L, | |
999999999999999999L, | |
Long.MAX_VALUE | |
}; | |
final static private char[] chars = new char[] { | |
'0', | |
'1', | |
'2', | |
'3', | |
'4', | |
'5', | |
'6', | |
'7', | |
'8', | |
'9', | |
}; | |
final static private char[] workspace = new char[20]; | |
public static void append(StringBuilder sb, int i) { | |
int ia = Math.abs(i); | |
if (i < 0) { | |
sb.append('-'); | |
} | |
int len = getStringLength(ia); | |
for (int n = len - 1; n >= 0; n--) { | |
workspace[n] = chars[ia % 10]; | |
ia /= 10; | |
} | |
sb.append(workspace, 0, len); | |
} | |
public static void append(StringBuilder sb, int i, char separator) { | |
int ia = Math.abs(i); | |
if (i < 0) { | |
sb.append('-'); | |
} | |
int len = getStringLength(ia); | |
len += (len - 1) / 3; | |
for (int n = len - 1; n >= 0; n--) { | |
if ((len - n) % 4 == 0) { | |
workspace[n] = separator; | |
} else { | |
workspace[n] = chars[ia % 10]; | |
ia /= 10; | |
} | |
} | |
sb.append(workspace, 0, len); | |
} | |
public static void append(StringBuilder sb, long l) { | |
long la = Math.abs(l); | |
if (l < 0) { | |
sb.append('-'); | |
} | |
int len = getStringLength(la); | |
for (int n = len - 1; n >= 0; n--) { | |
workspace[n] = chars[(int) (la % 10)]; | |
la /= 10; | |
} | |
sb.append(workspace, 0, len); | |
} | |
public static void append(StringBuilder sb, long i, char separator) { | |
long la = Math.abs(i); | |
if (i < 0) { | |
sb.append('-'); | |
} | |
int len = getStringLength(la); | |
len += (len - 1) / 3; | |
for (int n = len - 1; n >= 0; n--) { | |
if ((len - n) % 4 == 0) { | |
workspace[n] = separator; | |
} else { | |
workspace[n] = chars[(int) (la % 10)]; | |
la /= 10; | |
} | |
} | |
sb.append(workspace, 0, len); | |
} | |
public static void insert(StringBuilder sb, int index, int i) { | |
int ia = Math.abs(i); | |
if (i < 0) { | |
sb.insert(index++, '-'); | |
} | |
int len = getStringLength(ia); | |
for (int n = len - 1; n >= 0; n--) { | |
workspace[n] = chars[ia % 10]; | |
ia /= 10; | |
} | |
sb.insert(index, workspace, 0, len); | |
} | |
public static void insert(StringBuilder sb, int index, int i, char separator) { | |
int ia = Math.abs(i); | |
if (i < 0) { | |
sb.insert(index++, '-'); | |
} | |
int len = getStringLength(ia); | |
len += (len - 1) / 3; | |
for (int n = len - 1; n >= 0; n--) { | |
if ((len - n) % 4 == 0) { | |
workspace[n] = separator; | |
} else { | |
workspace[n] = chars[ia % 10]; | |
ia /= 10; | |
} | |
} | |
sb.insert(index, workspace, 0, len); | |
} | |
public static void insert(StringBuilder sb, int index, long l) { | |
long la = Math.abs(l); | |
if (l < 0) { | |
sb.insert(index++, '-'); | |
} | |
int len = getStringLength(la); | |
for (int n = len - 1; n >= 0; n--) { | |
workspace[n] = chars[(int) (la % 10)]; | |
la /= 10; | |
} | |
sb.insert(index, workspace, 0, len); | |
} | |
public static void insert(StringBuilder sb, int index, long i, char separator) { | |
long la = Math.abs(i); | |
if (i < 0) { | |
sb.insert(index++, '-'); | |
} | |
int len = getStringLength(la); | |
len += (len - 1) / 3; | |
for (int n = len - 1; n >= 0; n--) { | |
if ((len - n) % 4 == 0) { | |
workspace[n] = separator; | |
} else { | |
workspace[n] = chars[(int) (la % 10)]; | |
la /= 10; | |
} | |
} | |
sb.insert(index, workspace, 0, len); | |
} | |
private static int getStringLength(int i) { | |
int ia = Math.abs(i); | |
for (int n = 0; n < cutoffs.length; n++) { | |
if (ia <= cutoffs[n]) { | |
return n + 1; | |
} | |
} | |
return 0; | |
} | |
private static int getStringLength(long l) { | |
long la = Math.abs(l); | |
for (int n = 0; n < cutoffs.length; n++) { | |
if (la <= cutoffs[n]) { | |
return n + 1; | |
} | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment