KiCad PCB EDA Suite
Loading...
Searching...
No Matches
database_value.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <fmt/format.h>
23
24
25std::string NormalizeDecimalText( const std::string& aText )
26{
27 const size_t signLength = ( !aText.empty() && ( aText[0] == '-' || aText[0] == '+' ) ) ? 1 : 0;
28 const size_t point = aText.find( '.' );
29
30 if( point == std::string::npos )
31 return aText;
32
33 const std::string whole = aText.substr( signLength, point - signLength );
34 std::string fraction = aText.substr( point + 1 );
35
36 if( whole.empty() && fraction.empty() )
37 return aText;
38
39 if( whole.find_first_not_of( "0123456789" ) != std::string::npos
40 || fraction.find_first_not_of( "0123456789" ) != std::string::npos )
41 {
42 return aText;
43 }
44
45 fraction.erase( fraction.find_last_not_of( '0' ) + 1 );
46
47 if( fraction.empty() && whole.find_first_not_of( '0' ) == std::string::npos )
48 return "0";
49
50 std::string result = aText.substr( 0, signLength ) + ( whole.empty() ? "0" : whole );
51
52 if( !fraction.empty() )
53 result += "." + fraction;
54
55 return result;
56}
57
58
59std::string DatabaseValueToString( const DB_VALUE& aValue )
60{
61 struct VISITOR
62 {
63 std::string operator()( std::monostate ) const { return std::string(); }
64 std::string operator()( const std::string& aString ) const { return aString; }
65 std::string operator()( int64_t aInt ) const { return std::to_string( aInt ); }
66 std::string operator()( bool aBool ) const { return aBool ? "1" : "0"; }
67
68 std::string operator()( double aDouble ) const
69 {
70 // Negative zero must match the "0" that NormalizeDecimalText gives for "-0.000"
71 return aDouble == 0.0 ? std::string( "0" ) : fmt::format( "{}", aDouble );
72 }
73 };
74
75 return std::visit( VISITOR(), aValue );
76}
std::string NormalizeDecimalText(const std::string &aText)
Trim redundant trailing zeros and a dangling decimal point from decimal text, for example "4700000....
std::string DatabaseValueToString(const DB_VALUE &aValue)
Convert a database value to the text KiCad stores for it.
std::variant< std::monostate, std::string, int64_t, double, bool > DB_VALUE
A single database cell, where monostate is SQL NULL.
wxString result
Test unit parsing edge cases and error handling.