Add lexicalCast

This commit is contained in:
Vinnie Falco
2013-07-28 16:41:34 -07:00
parent 6002161b05
commit 760b04b0c4
9 changed files with 461 additions and 6 deletions

View File

@@ -192,6 +192,7 @@
<ClInclude Include="..\..\modules\beast_core\text\beast_CharPointer_UTF32.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_CharPointer_UTF8.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_Identifier.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_LexicalCast.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_LocalisedStrings.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_NewLine.h" />
<ClInclude Include="..\..\modules\beast_core\text\beast_String.h" />
@@ -748,6 +749,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\text\beast_LexicalCast.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\text\beast_LocalisedStrings.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>

View File

@@ -725,6 +725,9 @@
<ClInclude Include="..\..\modules\beast_core\misc\beast_Main.h">
<Filter>beast_core\misc</Filter>
</ClInclude>
<ClInclude Include="..\..\modules\beast_core\text\beast_LexicalCast.h">
<Filter>beast_core\text</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\beast_core.cpp">
@@ -1129,6 +1132,9 @@
<ClCompile Include="..\..\modules\beast_core\misc\beast_Main.cpp">
<Filter>beast_core\misc</Filter>
</ClCompile>
<ClCompile Include="..\..\modules\beast_core\text\beast_LexicalCast.cpp">
<Filter>beast_core\text</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="..\..\TODO.txt" />

View File

@@ -2,6 +2,8 @@
BEAST TODO
--------------------------------------------------------------------------------
- add expectThrow() to UnitTest, where it expects an exception
- Remove timeout forced-kill from Thread::stopThread ()
- Import secp256k1 from sipa

View File

@@ -193,7 +193,7 @@ namespace beast
#include "system/beast_SystemStats.cpp"
#include "text/beast_CharacterFunctions.cpp"
#include "text/beast_LexicalCast.cpp"
#include "text/beast_Identifier.cpp"
#include "text/beast_LocalisedStrings.cpp"
#include "text/beast_String.cpp"

View File

@@ -212,22 +212,25 @@ namespace beast
#include "memory/beast_Memory.h"
#include "text/beast_String.h"
#include "diagnostic/beast_SafeBool.h"
#include "diagnostic/beast_Error.h"
#include "diagnostic/beast_Debug.h"
#include "diagnostic/beast_Throw.h"
#include "text/beast_CharacterFunctions.h"
#include "text/beast_CharPointer_ASCII.h"
#include "text/beast_CharPointer_UTF16.h"
#include "text/beast_CharPointer_UTF32.h"
#include "text/beast_CharPointer_UTF8.h"
#include "text/beast_LexicalCast.h"
#include "time/beast_PerformedAtExit.h"
#include "diagnostic/beast_LeakChecked.h"
#include "memory/beast_ByteOrder.h"
#include "logging/beast_Logger.h"
#include "threads/beast_Thread.h"
#include "diagnostic/beast_Debug.h"
#include "diagnostic/beast_SafeBool.h"
#include "diagnostic/beast_Error.h"
#include "diagnostic/beast_FPUFlags.h"
#include "diagnostic/beast_Throw.h"
#include "diagnostic/beast_ProtectedCall.h"
#include "containers/beast_AbstractFifo.h"
#include "containers/beast_Array.h"

View File

@@ -26,7 +26,7 @@
the stack is unwound.
*/
template <class Exception>
inline void Throw (Exception const& e)
inline void Throw (Exception const& e, char const* = "", int = 0)
{
Debug::breakPoint ();

View File

@@ -0,0 +1,123 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
unsigned char const LexicalCastUtilities::s_digitTable [256] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xFF - 0x07
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08 - 0x0F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10 - 0x17
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18 - 0x1F
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20 - 0x27
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x28 - 0x2F
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30 - 0x37
0x08, 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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xC0 - 0xC7
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xC8 - 0xCF
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xD0 - 0xD7
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xD8 - 0xDF
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xE0 - 0xE7
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xE8 - 0xEF
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xF0 - 0xF7
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 0xF8 - 0xFF
};
//------------------------------------------------------------------------------
class LexicalCastTests : public UnitTest
{
public:
LexicalCastTests () : UnitTest ("LexicalCast", "beast")
{
}
template <class IntType>
static IntType nextRandomInt (Random& r)
{
return static_cast <IntType> (r.nextInt64 ());
}
template <class IntType>
void testInteger (IntType in)
{
String s;
IntType out;
expect (lexicalCastChecked (s, in));
expect (lexicalCastChecked (out, s));
expect (out == in);
}
template <class IntType>
void testIntegers (Random& r)
{
{
String s;
s << "random " << typeid (IntType).name ();
beginTestCase (s);
for (int i = 0; i < 1000; ++i)
{
IntType const value (nextRandomInt <IntType> (r));
testInteger (value);
}
}
{
String s;
s << "numeric_limits <" << typeid (IntType).name () << ">";
beginTestCase (s);
testInteger (std::numeric_limits <IntType>::min ());
testInteger (std::numeric_limits <IntType>::max ());
}
}
void runTest ()
{
int64 const seedValue = 50;
Random r (seedValue);
testIntegers <int> (r);
testIntegers <unsigned int> (r);
testIntegers <short> (r);
testIntegers <unsigned short> (r);
testIntegers <int32> (r);
testIntegers <uint32> (r);
testIntegers <int64> (r);
testIntegers <uint64> (r);
}
};
static LexicalCastTests lexicalCastTests;

View File

@@ -0,0 +1,303 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef BEAST_LEXICALCAST_H_INCLUDED
#define BEAST_LEXICALCAST_H_INCLUDED
// Base class with utility functions
struct LexicalCastUtilities
{
static unsigned char const s_digitTable [256];
// strict string to integer parser
template <class IntType, class InputIterator>
static inline bool parseSigned (IntType& result, InputIterator begin, InputIterator end)
{
if (0 == std::distance (begin, end))
return false;
uint64 accum = 0;
InputIterator it = begin;
// process sign
bool negative = false;
if ('+' == *it)
{
++it;
}
else if ('-' == *it)
{
++it;
negative = true;
}
if (end == it)
return false;
// calc max of abs value
uint64 max;
if (negative)
max = static_cast <uint64> (
-(static_cast <int64> (std::numeric_limits <IntType>::min ())));
else
max = std::numeric_limits <IntType>::max ();
// process digits
while (end != it)
{
uint64 const digit = static_cast <IntType> (
s_digitTable [static_cast <unsigned int> (*it++)]);
if (0xFF == digit)
return false;
uint64 const overflow = (max - digit) / 10;
if (accum > overflow)
return false;
accum = (10 * accum) + digit;
}
if (negative)
{
result = -static_cast <IntType> (accum);
}
else
{
result = static_cast <IntType> (accum);
}
return true;
}
template <class IntType, class InputIterator>
static inline bool parseUnsigned (IntType& result, InputIterator begin, InputIterator end)
{
if (0 == std::distance (begin, end))
return false;
uint64 accum = 0;
InputIterator it = begin;
uint64 const max = std::numeric_limits <IntType>::max ();
// process digits
while (end != it)
{
uint64 const digit = static_cast <IntType> (
s_digitTable [static_cast <unsigned int> (*it++)]);
if (0xFF == digit)
return false;
uint64 const overflow = (max - digit) / 10;
if (accum > overflow)
return false;
accum = (10 * accum) + digit;
}
result = static_cast <IntType> (accum);
return true;
}
};
//------------------------------------------------------------------------------
/** This is thrown when a conversion is not possible.
Only used in the throw variants of lexicalCast.
*/
struct BadLexicalCast : public std::bad_cast
{
};
// These specializatons get called by the non-member functions to do the work
template <class Out, class In>
struct LexicalCast;
// conversion to String
template <class In>
struct LexicalCast <String, In>
{
bool operator() (String& out, int in) const { out = String (in); return true; }
bool operator() (String& out, unsigned int in) const { out = String (in); return true; }
bool operator() (String& out, short in) const { out = String (in); return true; }
bool operator() (String& out, unsigned short in) const { out = String (in); return true; }
bool operator() (String& out, int64 in) const { out = String (in); return true; }
bool operator() (String& out, uint64 in) const { out = String (in); return true; }
bool operator() (String& out, float in) const { out = String (in); return true; }
bool operator() (String& out, double in) const { out = String (in); return true; }
};
// Parse String to number
template <class Out>
struct LexicalCast <Out, String>
{
bool operator() (int& out, String const& in) const { std::string const& s (in.toStdString ()); return LexicalCastUtilities::parseSigned (out, s.begin (), s.end ()); }
bool operator() (short& out, String const& in) const { std::string const& s (in.toStdString ()); return LexicalCastUtilities::parseSigned (out, s.begin (), s.end ()); }
bool operator() (int64& out, String const& in) const { std::string const& s (in.toStdString ()); return LexicalCastUtilities::parseSigned (out, s.begin (), s.end ()); }
bool operator() (unsigned int& out, String const& in) const { std::string const& s (in.toStdString ()); return LexicalCastUtilities::parseUnsigned (out, s.begin (), s.end ()); }
bool operator() (unsigned short& out, String const& in) const { std::string const& s (in.toStdString ()); return LexicalCastUtilities::parseUnsigned (out, s.begin (), s.end ()); }
bool operator() (uint64& out, String const& in) const { std::string const& s (in.toStdString ()); return LexicalCastUtilities::parseUnsigned (out, s.begin (), s.end ()); }
bool operator() (float& out, String const& in) const { bassertfalse; return false; /* UNIMPLEMENTED! */ }
bool operator() (double& out, String const& in) const { bassertfalse; return false; /* UNIMPLEMENTED! */ }
bool operator () (bool& out, String const& in) const
{
// boost::lexical_cast is very strict, it
// throws on anything but "1" or "0"
//
if (in == "1")
{
out = true;
return true;
}
else if (in == "0")
{
out = false;
return true;
}
return false;
}
};
//------------------------------------------------------------------------------
// Conversion to std::string
template <class In>
struct LexicalCast <std::string, In>
{
bool operator() (std::string& out, In in) const
{
String s;
if (LexicalCast <String, In> () (s, in))
{
out = s.toStdString ();
return true;
}
return false;
}
};
// Conversion from std::string
template <class Out>
struct LexicalCast <Out, std::string>
{
bool operator() (Out& out, std::string const& in) const
{
Out result;
if (LexicalCast <Out, String> () (result, String (in.c_str ())))
{
out = result;
return true;
}
return false;
}
};
// Conversion from null terminated char const*
template <class Out>
struct LexicalCast <Out, char const*>
{
bool operator() (Out& out, char const* in) const
{
Out result;
if (LexicalCast <Out, String> () (result, String (in)))
{
out = result;
return true;
}
return false;
}
};
// Conversion from null terminated char*
// The string is not modified.
template <class Out>
struct LexicalCast <Out, char*>
{
bool operator() (Out& out, char* in) const
{
Out result;
if (LexicalCast <Out, char const*> () (result, in))
{
out = result;
return true;
}
return false;
}
};
//------------------------------------------------------------------------------
/** Intelligently convert from one type to another.
@return `false` if there was a parsing or range error
*/
template <class Out, class In>
bool lexicalCastChecked (Out& out, In in)
{
return LexicalCast <Out, In> () (out, in);
}
/** Convert from one type to another, throw on error
An exception of type BadLexicalCast is thrown if the conversion fails.
@return The new type.
*/
template <class Out, class In>
Out lexicalCastThrow (In in)
{
Out out;
if (lexicalCastChecked (out, in))
return out;
Throw (BadLexicalCast (), __FILE__, __LINE__);
return Out ();
}
/** Convert from one type to another.
@param defaultValue The value returned if parsing fails
@return The new type.
*/
template <class Out, class In>
Out lexicalCast (In in, Out defaultValue = Out ())
{
Out out;
if (lexicalCastChecked (out, in))
return out;
return defaultValue;
}
#endif

View File

@@ -5,6 +5,9 @@ RIPPLE TODO
Vinnie's List: Changes day to day, descending priority
(Items marked '*' can be handled by others.)
- Show summary for text output of unit test results
- Make ProofOfWorkTests manual since they aren't used
* Make everyone check GitHub Issues every day
- Do something about the throw() reporting weaknesses:
* Make sure all Sconstruct and .pro builds have debug symbols in release
@@ -27,6 +30,14 @@ Vinnie's List: Changes day to day, descending priority
- Improved Mutex to track deadlocks
- Work on KeyvaDB
David Feature:
- override config items from command line
- change config via RPC, this is for debugging
--------------------------------------------------------------------------------
- Get rid of the WriteLog() stuff in the ripple tests and make it report the