Skip to content

Commit d8e0956

Browse files
committed
Address PR comments
1 parent 1d88f1e commit d8e0956

4 files changed

Lines changed: 198 additions & 101 deletions

File tree

codecs/codec-commons/src/main/java/software/amazon/smithy/java/codecs/commons/NumberCodec.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ public static int parseInt(byte[] buf, int start, int len) {
7676
if (buf[i] == '-') {
7777
negative = true;
7878
i++;
79-
if (i == end)
79+
if (i == end) {
8080
throw new NumberFormatException("Just a minus sign");
81+
}
8182
} else if (buf[i] == '+') {
8283
i++;
83-
if (i == end)
84+
if (i == end) {
8485
throw new NumberFormatException("Just a plus sign");
86+
}
8587
}
8688

8789
while (i < end - 1 && buf[i] == '0') {
@@ -97,19 +99,22 @@ public static int parseInt(byte[] buf, int start, int len) {
9799
byte[] limit = negative ? INT_MIN_DIGITS : INT_MAX_DIGITS;
98100
for (int j = 0; j < 10; j++) {
99101
int cmp = (buf[i + j] & 0xFF) - (limit[j] & 0xFF);
100-
if (cmp < 0)
102+
if (cmp < 0) {
101103
break;
102-
if (cmp > 0)
104+
}
105+
if (cmp > 0) {
103106
throw new NumberFormatException("Integer overflow");
107+
}
104108
}
105109
}
106110

107111
int result = 0;
108112

109113
while (i + 4 <= end) {
110114
int v = parse4Digits(buf, i);
111-
if (v < 0)
115+
if (v < 0) {
112116
break;
117+
}
113118
result = result * 10000 - v;
114119
i += 4;
115120
}
@@ -140,12 +145,14 @@ public static long parseLong(byte[] buf, int start, int len) {
140145
if (buf[i] == '-') {
141146
negative = true;
142147
i++;
143-
if (i == end)
148+
if (i == end) {
144149
throw new NumberFormatException("Just a minus sign");
150+
}
145151
} else if (buf[i] == '+') {
146152
i++;
147-
if (i == end)
153+
if (i == end) {
148154
throw new NumberFormatException("Just a plus sign");
155+
}
149156
}
150157

151158
while (i < end - 1 && buf[i] == '0') {
@@ -161,16 +168,18 @@ public static long parseLong(byte[] buf, int start, int len) {
161168

162169
while (i + 8 <= end) {
163170
long v = parse8Digits(buf, i);
164-
if (v < 0)
171+
if (v < 0) {
165172
break;
173+
}
166174
result = result * 100000000L - v;
167175
i += 8;
168176
}
169177

170178
while (i + 4 <= end) {
171179
int v = parse4Digits(buf, i);
172-
if (v < 0)
180+
if (v < 0) {
173181
break;
182+
}
174183
result = result * 10000 - v;
175184
i += 4;
176185
}
@@ -564,24 +573,33 @@ private static int writePaddedLong18(byte[] buf, int pos, long value) {
564573
}
565574

566575
public static int digitCount(int value) {
567-
if (value < 10)
576+
if (value < 10) {
568577
return 1;
569-
if (value < 100)
578+
}
579+
if (value < 100) {
570580
return 2;
571-
if (value < 1000)
581+
}
582+
if (value < 1000) {
572583
return 3;
573-
if (value < 10000)
584+
}
585+
if (value < 10000) {
574586
return 4;
575-
if (value < 100000)
587+
}
588+
if (value < 100000) {
576589
return 5;
577-
if (value < 1000000)
590+
}
591+
if (value < 1000000) {
578592
return 6;
579-
if (value < 10000000)
593+
}
594+
if (value < 10000000) {
580595
return 7;
581-
if (value < 100000000)
596+
}
597+
if (value < 100000000) {
582598
return 8;
583-
if (value < 1000000000)
599+
}
600+
if (value < 1000000000) {
584601
return 9;
602+
}
585603
return 10;
586604
}
587605

0 commit comments

Comments
 (0)