@@ -279,7 +279,29 @@ case class Uri(
279
279
def fragmentSegmentEncoding (encoding : Encoding ): Uri =
280
280
copy(fragmentSegment = fragmentSegment.map(f => f.encoding(encoding)))
281
281
282
- override def toString : String = {
282
+ //
283
+
284
+ /** Serializes the scheme to a string, without a trailing `:`. Might be an empty string, if no scheme is defined. */
285
+ def schemeToString : String = scheme.map(s => encode(Rfc3986 .Scheme )(s)).getOrElse(" " )
286
+
287
+ /** Serializes the path to a string, encoding the segments. A leading `/` is added if the path absolute. Might be an
288
+ * empty string, if the path is relative and empty.
289
+ */
290
+ def pathToString : String = {
291
+ val pathPrefixS = pathSegments match {
292
+ case _ if authority.isEmpty && scheme.isDefined => " "
293
+ case Uri .EmptyPath => " "
294
+ case Uri .AbsolutePath (_) => " /"
295
+ case Uri .RelativePath (_) => " "
296
+ }
297
+ val pathS = pathSegments.segments.map(_.encoded).mkString(" /" )
298
+ pathPrefixS + pathS
299
+ }
300
+
301
+ /** Serializes the query to a string, encoding the segments. The leading `?` is not included. Might be an empty
302
+ * string, if there's no query.
303
+ */
304
+ def queryToString : String = {
283
305
@ tailrec
284
306
def encodeQuerySegments (qss : List [QuerySegment ], previousWasPlain : Boolean , sb : StringBuilder ): String =
285
307
qss match {
@@ -298,24 +320,29 @@ case class Uri(
298
320
sb.append(kEnc(k)).append(" =" ).append(vEnc(v))
299
321
encodeQuerySegments(t, previousWasPlain = false , sb)
300
322
}
323
+ encodeQuerySegments(querySegments.toList, previousWasPlain = true , new StringBuilder ())
324
+ }
301
325
326
+ /** Serializes the fragment to a string, encoding the segment. The leading `#` is not included. Might be an empty
327
+ * string, if there's no fragment.
328
+ */
329
+ def fragmentToString : String = {
330
+ // https://stackoverflow.com/questions/2053132/is-a-colon-safe-for-friendly-url-use/2053640#2053640
331
+ fragmentSegment.fold(" " )(s => s.encoded)
332
+ }
333
+
334
+ override def toString : String = {
302
335
val schemeS = scheme.map(s => encode(Rfc3986 .Scheme )(s) + " :" ).getOrElse(" " )
303
336
val authorityS = authority.fold(" " )(_.toString)
304
- val pathPrefixS = pathSegments match {
305
- case _ if authority.isEmpty && scheme.isDefined => " "
306
- case Uri .EmptyPath => " "
307
- case Uri .AbsolutePath (_) => " /"
308
- case Uri .RelativePath (_) => " "
309
- }
310
- val pathS = pathSegments.segments.map(_.encoded).mkString(" /" )
311
- val queryPrefixS = if (querySegments.isEmpty) " " else " ?"
312
337
313
- val queryS = encodeQuerySegments(querySegments.toList, previousWasPlain = true , new StringBuilder ())
338
+ val pathS = pathToString
339
+
340
+ val queryPrefixS = if (querySegments.isEmpty) " " else " ?"
341
+ val queryS = queryToString
314
342
315
- // https://stackoverflow.com/questions/2053132/is-a-colon-safe-for-friendly-url-use/2053640#2053640
316
343
val fragS = fragmentSegment.fold(" " )(s => " #" + s.encoded)
317
344
318
- s " $schemeS$authorityS$pathPrefixS$ pathS$queryPrefixS$queryS$fragS"
345
+ s " $schemeS$authorityS$pathS$queryPrefixS$queryS$fragS"
319
346
}
320
347
}
321
348
0 commit comments