13

I working on a routine that parses a URI. Among obvious cases there is an empty string case. Is the empty string a valid input? What would be an outcome URI of an empty string?

Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159

3 Answers3

13

An empty string can’t possibly be a URI. The general URI syntax specifies that at least the scheme component followed by a : followed by the hier-part component (which may be empty) must be present.

But a relative URI reference can be empty. The syntax of relative references specifies that at least the relative-part component must be present, but it’s allowed to be empty (path-empty).

An empty relative URI reference is a same-document reference (bold emphasis mine):

The most frequent examples of same-document references are relative references that are empty or include only the number sign ("#") separator followed by a fragment identifier.

Community
  • 1
  • 1
unor
  • 92,415
  • 26
  • 211
  • 360
1

You can parse an empty String to a Uri without an exception but its fields will be null or their default invalid values.

val myUri = Uri.parse("")

myUri.host is null and myUri.port is -1

Akn
  • 421
  • 5
  • 9
0

Since the root of a URI is a limited set we know it does not have a valid root. It therefore does not identify anything and is invalid as an identifier. But this might have a meaning in your context still since tere is no specific standard for a URI.

Steven Stip
  • 387
  • 3
  • 11