Skip to content

Recognize blank-padded numeric strings as strnums - #573

Merged
bertysentry merged 2 commits into
mainfrom
571-numeric-strings-with-leading-or-trailing-blanks-are-not-recognized-as-strnums
Aug 18, 2026
Merged

Recognize blank-padded numeric strings as strnums#573
bertysentry merged 2 commits into
mainfrom
571-numeric-strings-with-leading-or-trailing-blanks-are-not-recognized-as-strnums

Conversation

@bertysentry

Copy link
Copy Markdown
Contributor

Fixes #571.

What changed

POSIX defines a numeric string as text that looks like a number after ignoring leading and trailing blanks, and gawk compares such input numerically. Jawk's strnum recognition rejected any blank, so ' 12 ' == 12 fell back to string comparison on $0, getline var results, split() pieces with a non-default separator, and preassigned variables.

  • JRT.isParseableNumber now skips leading whitespace and accepts trailing whitespace around the numeric token. Blank-only text, internal blanks (" 1 2 "), and trailing non-numeric text (" 12x ") are still rejected, and string constants are unaffected (they never were strnums).
  • StrNum.parseDoubleValue trims the same whitespace (new JRT.trimWhitespace) before parsing, so recognition and parsing move together — Double.parseDouble/BigDecimal don't accept every character Character.isWhitespace does.
  • The whitespace predicate is Character.isWhitespace, consistent with JRT.toDouble's leading-skip. gawk uses C isspace, so \f/\v padding also counts there (verified empirically); POSIX's space/tab blanks are a subset of both.

Verification

Every expectation was checked against gawk 5.0 first:

$ printf ' 12 \n' | gawk '{ print ($0 == 12), typeof($0) }'   # 1 strnum
$ printf ' 1 2 \n' | gawk '{ print ($0 == 12) }'              # 0
$ printf ' 12x \n' | gawk '{ print ($0 == 12) }'              # 0
$ printf '  \n'   | gawk '{ print ($0 == 0), ($0 == "") }'    # 0 0
$ gawk 'BEGIN { x = " 12 "; print (x == 12) }'                # 0
$ printf ' 0 \n'  | gawk '{ print ($0 ? "t" : "f") }'         # f
  • New unit tests in JRTComparisonNumberTest (recognition, locale separator, blank-padded StrNum parsing, uninitialized-vs-" 0 ").
  • New script tests in StrNumSemanticsTest: the issue's repro ($0 == 12 on " 12 "), getline var from a padded file, split() pieces, preassignments, truthiness of " 0 "/" 12 ", and the pinned non-strnum cases (internal blanks, " 12x ", blank-only records, string constants).
  • mvn verify green: 840 unit tests, 0 failures; checkstyle/pmd/spotbugs clean.
  • Compatibility suite diffed against a main worktree baseline: the failing-testcase sets are identical (141 both sides) — no regressions, and no fixtures move because the gawk strnum fixtures don't exercise blank padding (they already passed).
  • Behavior change recorded under Unreleased in behavior-changes.md.

🤖 Generated with Claude Code

POSIX defines a numeric string as text that looks like a number after
ignoring leading and trailing blanks, and gawk compares such input
numerically, so a record like " 12 " must compare equal to 12.
JRT.isParseableNumber now skips leading whitespace and accepts trailing
whitespace around the numeric token, and StrNum.parseDoubleValue trims
the same whitespace before parsing, so recognition and parsing move
together. Blank-only text, internal blanks, trailing non-numeric text,
and string constants remain non-numeric, as POSIX specifies.

Fixes #571

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: da6d83e39e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1229 to +1233
while (start < length && Character.isWhitespace(value.charAt(start))) {
start++;
}
int end = numericPrefixEnd(value, start, decimalSeparator);
return end > start && isBlankToEnd(value, end);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict strnum padding to AWK whitespace

When an input-derived value is padded with Java-only whitespace such as U+2003 EM SPACE or U+001C FILE SEPARATOR, Character.isWhitespace causes it to be recognized and trimmed as a number. For example, "\u200312\u2003" now compares equal to 12, whereas gawk 5.2.1 under C.UTF-8 reports it as a non-equal string; this can change comparisons and truthiness for Unicode input. Use the AWK/gawk whitespace set for both the leading scan and trailing check rather than Java's broader predicate.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This divergence is deliberate, per Jawk's compatibility policy: on locale-dependent edges Jawk applies Java semantics rather than emulating the C locale's isspace set — the same way JRT.toDouble already skips leading Unicode whitespace in string-to-number conversion. Keeping recognition on Character.isWhitespace keeps it consistent with conversion and guarantees everything recognized also parses (JRT.trimWhitespace uses the same predicate). Documented the divergence in the deliberate-differences list on the index page and pinned it with a test in 5e81af5.

Character.isWhitespace is wider than the C isspace set gawk relies on,
so Unicode spaces such as EM SPACE pad a numeric string in Jawk but not
in gawk. This is deliberate: Jawk applies Java semantics on such
locale-dependent edges, consistently with the leading-whitespace skip
in string-to-number conversion. Document the divergence on the index
page and pin it with a test so recognition and parsing stay aligned on
the wider set.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@bertysentry

Copy link
Copy Markdown
Contributor Author

@codex please review again

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 5e81af503c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@bertysentry
bertysentry merged commit 5cef740 into main Aug 18, 2026
5 checks passed
@bertysentry
bertysentry deleted the 571-numeric-strings-with-leading-or-trailing-blanks-are-not-recognized-as-strnums branch August 18, 2026 12:31
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.

Numeric strings with leading or trailing blanks are not recognized as strnums

1 participant