Switch from strtod to std::from_chars for performance

This commit is contained in:
Marek Roszko 2022-08-13 22:28:41 -04:00 committed by Mark Roszko
parent 90a62d8b02
commit a070959209
5 changed files with 55 additions and 36 deletions

View File

@ -23,6 +23,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <charconv>
#include <wx/ffile.h>
#include <wx/log.h>
#include <eda_item.h>
@ -848,9 +849,20 @@ double DRAWING_SHEET_PARSER::parseDouble()
if( token != T_NUMBER )
Expecting( T_NUMBER );
double val = strtod( CurText(), NULL );
double dval{};
const std::string& str = CurStr();
std::from_chars_result res = std::from_chars( str.data(), str.data() + str.size(), dval );
return val;
if( res.ec != std::errc() )
{
wxString error;
error.Printf( _( "Invalid floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
CurSource(), CurLineNumber(), CurOffset() );
THROW_IO_ERROR( error );
}
return dval;
}
// defaultDrawingSheet is the default drawing sheet using the S expr.

View File

@ -16,9 +16,9 @@
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <macros.h>
#include <base_units.h>
#include <charconv>>
#include <string_utils.h>
#include <eda_rect.h>
#include <render_settings.h>
@ -316,9 +316,20 @@ double STROKE_PARAMS_PARSER::parseDouble( const char* aText )
if( token != T_NUMBER )
Expecting( aText );
double val = strtod( CurText(), NULL );
double dval{};
const std::string& str = CurStr();
std::from_chars_result res = std::from_chars( str.data(), str.data() + str.size(), dval );
return val;
if( res.ec != std::errc() )
{
wxString error;
error.Printf( _( "Invalid floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
CurSource(), CurLineNumber(), CurOffset() );
THROW_IO_ERROR( error );
}
return dval;
}

View File

@ -27,6 +27,8 @@
// For some reason wxWidgets is built with wxUSE_BASE64 unset so expose the wxWidgets
// base64 code.
#include <charconv>
#define wxUSE_BASE64 1
#include <wx/base64.h>
#include <wx/mstream.h>
@ -422,8 +424,6 @@ LIB_ITEM* SCH_SEXPR_PARSER::ParseDrawItem()
double SCH_SEXPR_PARSER::parseDouble()
{
char* tmp;
// In case the file got saved with the wrong locale.
if( strchr( CurText(), ',' ) != nullptr )
{
@ -431,23 +431,17 @@ double SCH_SEXPR_PARSER::parseDouble()
CurLine(), CurLineNumber(), CurOffset() );
}
errno = 0;
double dval{};
const std::string& str = CurStr();
std::from_chars_result res = std::from_chars( str.data(), str.data() + str.size(), dval );
double fval = strtod( CurText(), &tmp );
if( errno )
if( res.ec != std::errc() )
{
THROW_PARSE_ERROR( _( "Invalid floating point number" ), CurSource(), CurLine(),
CurLineNumber(), CurOffset() );
}
if( CurText() == tmp )
{
THROW_PARSE_ERROR( _( "Missing floating point number" ), CurSource(), CurLine(),
CurLineNumber(), CurOffset() );
}
return fval;
return dval;
}

View File

@ -22,6 +22,7 @@
*/
#include <board_design_settings.h>
#include <charconv>
#include <convert_to_biu.h>
#include <layer_ids.h>
#include <macros.h>
@ -659,9 +660,20 @@ double PCB_PLOT_PARAMS_PARSER::parseDouble()
if( token != T_NUMBER )
Expecting( T_NUMBER );
double val = strtod( CurText(), nullptr );
double dval{};
const std::string& str = CurStr();
std::from_chars_result res = std::from_chars( str.data(), str.data() + str.size(), dval );
return val;
if( res.ec != std::errc() )
{
wxString error;
error.Printf( _( "Invalid floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
CurSource(), CurLineNumber(), CurOffset() );
THROW_IO_ERROR( error );
}
return dval;
}

View File

@ -28,6 +28,7 @@
*/
#include <cerrno>
#include <charconv>
#include <confirm.h>
#include <macros.h>
#include <title_block.h>
@ -176,13 +177,11 @@ void PCB_PARSER::pushValueIntoMap( int aIndex, int aValue )
double PCB_PARSER::parseDouble()
{
char* tmp;
double dval{};
const std::string& str = CurStr();
std::from_chars_result res = std::from_chars( str.data(), str.data() + str.size(), dval );
errno = 0;
double fval = strtod( CurText(), &tmp );
if( errno )
if( res.ec != std::errc() )
{
wxString error;
error.Printf( _( "Invalid floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
@ -191,16 +190,7 @@ double PCB_PARSER::parseDouble()
THROW_IO_ERROR( error );
}
if( CurText() == tmp )
{
wxString error;
error.Printf( _( "Missing floating point number in\nfile: '%s'\nline: %d\noffset: %d" ),
CurSource(), CurLineNumber(), CurOffset() );
THROW_IO_ERROR( error );
}
return fval;
return dval;
}