BDE 4.14.0 Production release
|
Provide functions that operate on IndexSpan
objects.
IndexSpan
This component provides a struct, IndexSpanUtil
, that serves as a namespace for utility functions that operate on IndexSpan
objects. At the moment the only function provided is shrink
, and it creates a new IndexSpan
that represents a (possibly) smaller span than the argument.
This section illustrates intended use of this component.
Suppose we have a class that stores a parsed URL using a string to store the full URL and IndexSpan
objects to describe the individual parts of the URL, and we want to add accessors that handle the case when the host part of the URL is an IPv6 address, such as "http://[ff:fe:9]/index.html". As observed, an IPv6 address is indicated by the [
and ]
characters (the URL is ill formed if the closing ]
is not present). We want to implement two methods, one to query if the host part of the URL is IPv6 (isIPv6Host
) and another to get the IPv6 address (the part without the square brackets) if the host is actually an IPv6 address (getIPv6Host
).
First, let us create a ParsedUrl
class. For brevity, the class has only those parts that are needed to implement isIPv6Host
and getIPv6Host
.
Next, we implement isIPv6Host
.
Then, to make the accessors simple (and readable), we implement a helper function that creates a StringRef
from a StringRef
and an IndexSpan
. (Don't do this in real code, use IndexSpanStringUtil::bind
that is levelized above this component - so we cannot use it here.)
Next, we implement getIPv6Host
using bdlb::IndexSpanUtil::shrink
.
Finally, we verify the two methods with URLs.