Skip to content

Commit

Permalink
Patch for URI
Browse files Browse the repository at this point in the history
  • Loading branch information
cpurdy committed Jan 11, 2025
1 parent 45e045f commit 2a313c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
11 changes: 7 additions & 4 deletions lib_net/src/main/x/net/Uri.x
Original file line number Diff line number Diff line change
Expand Up @@ -832,24 +832,27 @@ const Uri
Int endOffset = totalLength;
if (isLast && end.offset < endOffset) {
endOffset = end.offset;
if (offset == endOffset) {
return True;
}
}
if (String prefix ?= section.prefix) {
Int partLen = prefix.size;
if (offset < partLen) {
prefix[offset..<endOffset.notGreaterThan(partLen)].appendTo(buf);
}
offset = (offset-partLen).notLessThan(0);
endOffset -= partLen;
endOffset = (endOffset-partLen).notLessThan(0);
}
if (!text.empty) {
if (offset < endOffset && !text.empty) {
Int partLen = text.size;
if (offset < partLen) {
section.escape(buf, text[offset..<endOffset.notGreaterThan(partLen)]);
}
offset = (offset-partLen).notLessThan(0);
endOffset -= partLen;
endOffset = (endOffset-partLen).notLessThan(0);
}
if (String suffix ?= section.suffix) {
if (offset < endOffset, String suffix ?= section.suffix) {
Int partLen = suffix.size;
if (offset < partLen) {
suffix[offset..<endOffset.notGreaterThan(partLen)].appendTo(buf);
Expand Down
35 changes: 29 additions & 6 deletions lib_net/src/main/x/net/UriTemplate.x
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ const UriTemplate {
@Override
@RO Char prefix;

@RO Value defaultValue.get() = "";

@RO Value defaultExplodedValue.get() {
static String[] EmptyArray = [];
return EmptyArray;
}

@Override
Appender<Char> expand(Appender<Char> buf, Lookup values) {
for (Variable var : vars) {
Expand Down Expand Up @@ -482,24 +489,34 @@ const UriTemplate {
@Override
conditional (Position after, Map<String, Value> bindings) matches(Uri uri,
Position from, Position? to, Char? nextPrefix, Map<String, Value> bindings) {
// we can only match if we are inside the correct section
if (!(from := uri.positionAt(from, onlyWithin))) {
return False;
}

String sectionText = uri.sectionText(onlyWithin);
Int sectionLength = sectionText.size;
if (sectionLength == 0) {
if (prefix == nextPrefix) {
// this is a conservative decision, but will not always be correct; the
// assumption here is that a multi-part section can't have multiple parts
// if it doesn't have any parts; it may need to be re-worked if a use case
// arises where multiple empty parts are desired
return False;
}
for (Variable var : vars) {
if (var.require) {
// no text, no match (because even the prefix is absent)
return False;
} else {
bindings = mutate(bindings).put(var.name, var.explode ? defaultExplodedValue : defaultValue);
}
}
return True, from, bindings;
}

// we can only match if we are inside the correct section
if (!(from := uri.positionAt(from, onlyWithin))) {
return False;
}
Int fromOffset = from.offset;

Int toOffset = sectionLength;
Int toOffset = sectionLength;
to := uri.positionAt(to?, onlyWithin);
switch (to?.section <=> onlyWithin) {
case Lesser:
Expand Down Expand Up @@ -613,6 +630,12 @@ const UriTemplate {
return '?';
}

@Override
Value defaultExplodedValue.get() {
static Map<String,String> EmptyMap = [];
return EmptyMap;
}

@Override
conditional (Position after, Map<String, Value> bindings) matches(Uri uri,
Position from, Position? to, Char? nextPrefix, Map<String, Value> bindings) {
Expand Down

0 comments on commit 2a313c0

Please sign in to comment.