MCPcopy Create free account
hub / github.com/questdb/questdb / parseIPv4_0

Method parseIPv4_0

core/src/main/java/io/questdb/std/Numbers.java:1230–1288  ·  view source on GitHub ↗
(CharSequence sequence, final int p, int lim)

Source from the content-addressed store, hash-verified

1228 }
1229
1230 public static int parseIPv4_0(CharSequence sequence, final int p, int lim) throws NumericException {
1231 if (lim == 0) {
1232 throw NumericException.instance().put("empty IPv4 address string");
1233 }
1234
1235 int hi;
1236 int lo = p;
1237 int num;
1238 int ipv4 = 0;
1239 int count = 0;
1240
1241 final char sign = sequence.charAt(lo);
1242
1243 // removes any leading dots
1244 if (notDigit(sign)) {
1245 if (sign == '.') {
1246 do {
1247 lo++;
1248 } while (sequence.charAt(lo) == '.');
1249 } else {
1250 throw NumericException.instance().put("invalid IPv4 address: ").put(sequence);
1251 }
1252 }
1253
1254 while ((hi = Chars.indexOf(sequence, lo, '.')) > -1 && count < 3) {
1255 num = parseInt(sequence, lo, hi);
1256 if (num > 255) {
1257 throw NumericException.instance().put("IPv4 octet out of range [0-255]: ").put(num);
1258 }
1259 ipv4 = (ipv4 << 8) | num;
1260 count++;
1261 lo = hi + 1;
1262 }
1263
1264 if (count != 3) {
1265 throw NumericException.instance().put("IPv4 address must have 4 octets, found: ").put(count + 1);
1266 }
1267
1268 // removes any trailing dots
1269 if ((hi = Chars.indexOf(sequence, lo, '.')) > -1) {
1270 num = parseInt(sequence, lo, hi);
1271 hi++;
1272 while (hi < lim) {
1273 if (sequence.charAt(hi) == '.') {
1274 hi++;
1275 } else {
1276 throw NumericException.instance().put("invalid character in IPv4 address: ").put(sequence);
1277 }
1278 }
1279 } else {
1280 num = parseInt(sequence, lo, lim);
1281 }
1282
1283 if (num > 255) {
1284 throw NumericException.instance().put("IPv4 octet out of range [0-255]: ").put(num);
1285 }
1286
1287 return (ipv4 << 8) | num;

Callers 12

testParseIPv4_0Method · 0.95
testParseIPv4_0EmptyMethod · 0.95
testParseIPv4_0NullMethod · 0.95
getIPv4SubnetMethod · 0.95
parseIPv4Method · 0.95
parseIPv4UDPMethod · 0.95

Calls 6

instanceMethod · 0.95
notDigitMethod · 0.95
indexOfMethod · 0.95
parseIntMethod · 0.95
putMethod · 0.65
charAtMethod · 0.45

Tested by 9

testParseIPv4_0Method · 0.76
testParseIPv4_0EmptyMethod · 0.76
testParseIPv4_0NullMethod · 0.76