On 6/16/2015 5:18 PM, Jason C. McDonald wrote:
> I am trying to write a function that can convert an integer to its
> equivalent Ustring (or, arguably, most any string class), without having
> to rely on the STL or third-party libraries. I'm also wanting to use
> math as much as possible, so I don't have to use massive conditionals or
> data structures.
>
> This code IS working quite well, but I feel like it can be improved. Any
> feedback, improvements, or whatnot?
1) what is a UString?
2) what's wrong with the STL?
2a) What's wrong with std::string?
3) Why would you want to use math for something that can be done a few
lines with the STL?
4) Massive conditionals and data structures? huh?
All of the improvements I would suggest would be to throw away the
aforementioned constraints. You really shouldn't make constraints
without very good reason. I can't say that I've heard a good reason for
using C++ without the STL to date.
Your statement is like, "I want to do things better, but I can't use the
better way of doing things to do it."
Here is my version:
//------------------------------------------------------------------------------
std::string IntToString(const long long & value)
{
std::ostringstream converter;
converter << value;
return converter.str();
}
//------------------------------------------------------------------------------
std::string UIntToString(const unsigned long long & value)
{
std::ostringstream converter;
converter << value;
return converter.str();
}
//--------------------------------------------------------------------------------------------------
int StringToInt(const std::string & value)
{
int result;
std::istringstream converter(value);
if( (converter >> result).fail() )
{
std::ostringstream msg;
msg << "Could not convert string: \"" << value << "\" to int";
throw Shared::Exception(__FILE__, __LINE__, msg.str()); // Use
your favorite exception here
}
return result;
}
//--------------------------------------------------------------------------------------------------
boost::optional<int> StringToNullableInt(const std::string & value)
{
if( value.empty() )
{
return boost::none;
}
return Shared::StringToInt(value);
}
etc. etc.
Which looks more readable and maintainable?
Feel free to use the C library functions instead of you are more
interested in saving the bit of performance over using streams, but are
sure to do the proper error checking.
Another alternative is boost::lexical_cast.
EDIT:
After Googling ustring, I'd opt to use std::string internally and call
the glib conversions when I needed UTF-8 only when a call was expecting
UTF-8. I am not a fan of "X uses unicode, so let's use different string
representations in our entire program." It makes messes.
Furthermore, it looks like yer glib::ustring has stream operators
anyway, so you can use those in the same manner.
--
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---