altos: Shrink ao_cmd_put16, ao_cmd_hex and ao_cmd
[fw/altos] / altosui / Altos.java
index 25d97bcf2b5c6fc35c145c201e12ee5c440caea1..364908448e2d2fcbdab39d32f71abf0f74281394 100644 (file)
@@ -231,10 +231,27 @@ public class Altos {
        static final Charset    unicode_set = Charset.forName("UTF-8");
 
        static String string(int[] bytes, int s, int l) {
-               byte[]  b = new byte[bytes.length];
-               for (int i = 0; i < l; i++)
+               if (s + l > bytes.length) {
+                       if (s > bytes.length) {
+                               s = bytes.length;
+                               l = 0;
+                       } else {
+                               l = bytes.length - s;
+                       }
+               }
+
+               int i;
+               for (i = l - 1; i >= 0; i--)
+                       if (bytes[s+i] != 0)
+                               break;
+
+               l = i + 1;
+               byte[]  b = new byte[l];
+
+               for (i = 0; i < l; i++)
                        b[i] = (byte) bytes[s+i];
-               return new String(b, unicode_set);
+               String n = new String(b, unicode_set);
+               return n;
        }
 
        static int hexbyte(String s, int i) {