25
5
Your network scanning tool is annoyingly picky about input, and immediately crashes if you feed it an IPv4 address that contains improper characters or isn't properly formatted.
An IPv4 address is a 32-bit numeric address written as four numbers separated by periods. Each number can be zero to 255.
We need to write a tool to pre-validate the input to avoid those crashes, and our specific tool is picky: A valid format will look like a.b.c.d
where a, b, c and d:
- Can be a
0
or a natural number with no leading zeros. - Should be between 0 - 255 (inclusive).
- Should not contain special symbols like
+
,-
,,
, and others. - Should be decimal (base
10
)
Input: A string
Output: Truthy or Falsey value (arbitrary values also accepted)
Test Cases:
Input | Output | Reason
| |
- 1.160.10.240 | true |
- 192.001.32.47 | false | (leading zeros present)
- 1.2.3. | false | (only three digits)
- 1.2.3 | false | (only three digits)
- 0.00.10.255 | false | (leading zeros present)
- 1.2.$.4 | false | (only three digits and a special symbol present)
- 255.160.0.34 | true |
- .1.1.1 | false | (only three digits)
- 1..1.1.1 | false | (more than three periods)
- 1.1.1.-0 | false | (special symbol present)
- .1.1.+1 | false | (special symbol present)
- 1 1 1 1 | false | (no periods)
- 1 | false | (only one digit)
- 10.300.4.0 | false | (value over 255)
- 10.4F.10.99 | false | (invalid characters)
- fruit loops | false | (umm...)
- 1.2.3.4.5 | false | (too many periods/numbers)
- 0.0.0.0 | true |
- 0.0 0.0. | false | (periods misplaced)
- 1.23..4 | false | (a typo of 1.2.3.4)
- 1:1:1:1:1:1:1:1| false | (an IPv6 address, not IPv4)
This is code-golf, so fewest bytes will win!
Note for the users - if you want to add some more test-cases, you're welcomed (by suggesting an edit). But, please make sure that the test-cases don't repeat themselves! Thanks
10Suggest testcases:
1.1.1.1.1
,1.1.1.1.
,.1.1.1
,1..1.1
,1..1.1.1
,1.1.1.0
,1.1.1.-0
,1.1.1.+1
,1.1.1.1E1
,1.1.1.256
,1.1.1.0x1
,255.255.255.255
,0.0.0.0
,'or 1=1--
,<empty string>
,1 1 1 1
,1,1,1,1
. – tsh – 2018-10-22T09:11:04.900I'd suggest test cases like
Hello, World!
,1.1.1.1.1
,1.-1.1.1
– Jo King – 2018-10-22T10:37:09.3005Suggest adding test cases "1.2.3.4.5" (to rule out too long IPs) and "999.0.0.0" (to rule out too large IPs). – Triggernometry – 2018-10-22T15:50:02.080
5Possibly slightly picky, but you should probably refer to "IPv4 addresses" rather than "IP addresses" - or at least, mention somewhere that you just mean IPv4 addresses - otherwise 1234:5678::1 ought to be a valid IP address (whereas from the description it's clear that that's not intended :) – psmears – 2018-10-23T15:51:27.053
1Why are leading 0's disallowed in octets especially if one of your restrictions is base 10? – Poke – 2018-10-23T18:08:48.730
0 is a valid IPv4 address too, which expands to 127.0.0.1 – Criggie – 2018-10-23T18:42:48.330
1.2.3 is accepted by ping too - it expands to 1.2.0.3 and 1.2 expands to 1.0.0.2 – Criggie – 2018-10-23T18:43:31.233
3@Criggie The premise isn't to actually check all real IP4 rules (like the ones you mentioned), it's to ensure that the input string doesn't crash some other (presumably badly written) app that only allows input in a very specific form. Also, we're not going to change the rules of a challenge that already has 30+ answers. – BradC – 2018-10-23T19:03:52.980
@BradC Fair enough - I'm just pointing out that this is not 100% reflective of the real world. Its a bit specalised. – Criggie – 2018-10-23T19:06:37.420
2@Criggie Worth noting that the RFC declares that "Addresses are fixed length of four octets". I think the fringe cases you're referencing are more specialized than this challenge. – Poke – 2018-10-23T19:12:51.900
1A test case similar to one presented by @tsh would be
1.23..4
(e.g. a typo of1.2.3.4
); there are four digits and three periods, and there are no leading or trailing periods, but it's still not valid (other than via Criggie's expansion, which would still be a different IP from what was likely intended:1.23.0.4
vs1.2.3.4
) – Doktor J – 2018-10-23T19:24:33.5932
Ipv4 addresses can also be written as a longint... such as http://16843009, should that be allowed here?
– nl-x – 2018-10-24T09:41:52.9572@nl-x It very heavily seems like the intent of this challenge is to parse ip addresses in dot-decimal notation. – Poke – 2018-10-24T14:44:21.460
To IP or not to IP? – Magic Octopus Urn – 2019-01-29T17:57:56.947
“fruit loops” (umm...) – Stan Strum – 2019-02-10T22:00:09.697