Skip to content

Comments

Added a substring test case for an empty string and zero index.#55

Open
evan-gordon wants to merge 1 commit intocqframework:mainfrom
evan-gordon:main
Open

Added a substring test case for an empty string and zero index.#55
evan-gordon wants to merge 1 commit intocqframework:mainfrom
evan-gordon:main

Conversation

@evan-gordon
Copy link
Contributor

From looking at the docs it was a bit unclear if this case should return empty string or null, but empty string made the most sense to me. Happy to discuss this further.

Related to @suyashkumar 's changes in google/cql#117

@raleigh-g-thompson
Copy link
Contributor

raleigh-g-thompson commented Jun 6, 2025

I agree the SubString documentation, https://cql.hl7.org/09-b-cqlreference.html#substring, is a bit vague.

The key is the line
If stringToSub or startIndex is null, or startIndex is out of range, the result is null.

What is meant by startIndex is out of range? And what is the range of an empty string?

Does an empty string have a range since it is length zero? I'd say there is no range hence any value for startIndex will be out or range.

So the expected behavior, using my interpretation, is that substring should return null for an empty string.

That being said, looking at how other languages treat substring applied to an empty string

  • python throws an exception IndexError: string index out of range
  • javascript returns an empty string
  • c# returns an empty string
  • java returns an empty string

Should CQL follow the 'common' convention and return an empty string?

@suyashkumar
Copy link
Contributor

I agree that the spec as written does best support the null return, as I mentioned in the original PR here google/cql#117 (comment). I don't feel strongly, but I think it's worth some additional consideration mostly because the null return scenario means that

Substring(str, 0) == str

Will not always hold true, which I find a bit odd. That said, it's probably not a big deal either way in practice so more of a definition thing and something worth adding a spec test for (whichever way we decide). I don't feel super strongly either way personally, but lean towards the empty string return. I figured it was worth some quick discussion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants