openframeworks / openframeworks/openFrameworks
ofToInt, ofToFloat, etc conflate error status with valid values
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
The ofToX family of functions return a value of 0 (or 0.0) on error even though that is also a valid conversion value. Also, for ofToInt and ofToInt64 functions, the documentation is not accurate since an out of range string will produce the min/max integer value as the error code, not zero.
For example, the following code:
printf("%d\n", ofToInt("0"));
printf("%d\n", ofToInt(""));
printf("%d\n", ofToInt("99999999999"));
outputs the following:
0
0
2147483647
Line 1 and line 2 return the same value, even though line 1 succeeds and line 2 fails. Line 3 fails but returns max int instead of zero.
For a solution, I was thinking of something like the following, which would avoid breaking the current API:
template<typename T>
T ofTo(const std::string & str, bool *fail = nullptr){
T x;
std::istringstream cur(str);
cur >> x;
if (fail) {
*fail = iss.fail();
}
return x;
}
The return value would not change, but the optional "fail" flag would be set to indicate a conversion error. If this is acceptable, I would be happy to create a PR.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the ofToX family definitions and its documentation, then inspect existing tests or call sites for conversion errors. Verify how zero and out-of-range results are currently reported, and define a compatible way to distinguish successful zero conversions from failures while correcting the documented behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100