// The portion of the pathinfo which follows the third slash is the
// subpath of the request. An empty sting is equivalent to having no
// subpath. Trailing and successive slashes are dropped.
- char const* sos = ndx[0];
char const* sep = nullptr;
size_t n·s = 0;
- while (end[0] > sos) {
+ for (
+ char const* sos = ndx[0];
+ sos < end[0];
+ sos = (end[0] > sep ? sep + 1 : end[0])
+ ) {
// Count the number of segments in the pathinfo so that the correct
// amount of space can be allocated.
sep = strchr(sos, '/');
if (sep > sos) {
++n·s;
}
- if (end[0] > sep) {
- sos = sep + 1;
- } else {
- sos = end[0];
- }
- }
- req.subpath = calloc(n·s + 1, sizeof(char*));
- if (!req.subpath) {
- return req;
}
- size_t i·s = 0;
- while (end[0] > ndx[0]) {
- // Add the segments to the newly allocated array.
- sep = strchr(ndx[0], '/');
- if (!sep) {
- sep = end[0];
+ if (n·s > 0) {
+ req.subpath = calloc(n·s + 1, sizeof(char*));
+ if (!req.subpath) {
+ return req;
}
- if (sep > ndx[0]) {
- req.subpath[i·s++] = strndup(ndx[0], sep - ndx[0]);
- }
- if (end[0] > sep) {
- ndx[0] = sep + 1;
- } else {
- ndx[0] = end[0];
+ for (
+ size_t i·s = 0;
+ i·s < n·s;
+ ndx[0] = (end[0] > sep ? sep + 1 : end[0])
+ ) {
+ // Add the segments to the newly allocated array.
+ sep = strchr(ndx[0], '/');
+ if (!sep) {
+ sep = end[0];
+ }
+ if (sep > ndx[0]) {
+ req.subpath[i·s++] = strndup(ndx[0], sep - ndx[0]);
+ }
}
+ req.subpath[n·s] = nullptr;
}
- assert(i·s == n·s);
- req.subpath[i·s] = nullptr;
// Return the result.
return req;