FromString.hpp
Go to the documentation of this file.
1 //
2 // Balau core C++ library
3 //
4 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
5 //
6 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
7 // See the LICENSE file for the full license text.
8 //
9 
15 
16 #ifndef COM_BORA_SOFTWARE__BALAU_APPLICATION__FROM_STRING
17 #define COM_BORA_SOFTWARE__BALAU_APPLICATION__FROM_STRING
18 
19 #include <Balau/Type/Impl/FromStringImpl.hpp>
20 #include <Balau/Util/Strings.hpp>
21 
23 
30 inline void fromString(std::string & destination, std::string_view value) {
31  destination = std::string(value.data(), value.length());
32 }
33 
40 inline void fromString(std::u16string & destination, std::string_view value) {
41  destination = boost::locale::conv::utf_to_utf<char16_t, char>(std::string(value));
42 }
43 
50 inline void fromString(std::u32string & destination, std::string_view value) {
51  destination = boost::locale::conv::utf_to_utf<char32_t, char>(std::string(value));
52 }
53 
66 inline void fromString(char & destination, std::string_view value) {
67  Balau::Impl::validateFromStringValue(value);
68  int offset = 0;
69  char32_t c = Balau::Character::getNextUtf8Safe(value, offset);
70  Balau::Impl::validateCharacter(c);
71 
72  if (c > 255) {
75  , toString("Cannot convert code point ", (long long) c, " to ascii char.")
76  );
77  }
78 
79  destination = static_cast<char>(c);
80 }
81 
93 inline void fromString(char32_t & destination, std::string_view value) {
94  Balau::Impl::validateFromStringValue(value);
95  int offset = 0;
96  destination = Balau::Character::getNextUtf8Safe(value, offset);
97  Balau::Impl::validateCharacter(destination);
98 }
99 
112 inline void fromString(signed char & destination, std::string_view value) {
113  long n = Balau::Impl::stringToLong(value);
114  destination = Balau::Impl::safeNumericCast<signed char>(n);
115 }
116 
130 inline void fromString(signed char & destination, std::string_view value, int base) {
131  long n = Balau::Impl::stringToLong(value, base);
132  destination = Balau::Impl::safeNumericCast<signed char>(n);
133 }
134 
147 inline void fromString(short & destination, std::string_view value) {
148  long n = Balau::Impl::stringToLong(value);
149  destination = Balau::Impl::safeNumericCast<short>(n);
150 }
151 
165 inline void fromString(short & destination, std::string_view value, int base) {
166  long n = Balau::Impl::stringToLong(value, base);
167  destination = Balau::Impl::safeNumericCast<short>(n);
168 }
169 
182 inline void fromString(int & destination, std::string_view value) {
183  long n = Balau::Impl::stringToLong(value);
184  destination = Balau::Impl::safeNumericCast<int>(n);
185 }
186 
200 inline void fromString(int & destination, std::string_view value, int base) {
201  long n = Balau::Impl::stringToLong(value, base);
202  destination = Balau::Impl::safeNumericCast<int>(n);
203 }
204 
217 inline void fromString(long & destination, std::string_view value) {
218  destination = Balau::Impl::stringToLong(value);
219 }
220 
234 inline void fromString(long & destination, std::string_view value, int base) {
235  destination = Balau::Impl::stringToLong(value, base);
236 }
237 
250 inline void fromString(long long & destination, std::string_view value) {
251  destination = Balau::Impl::stringToLongLong(value);
252 }
253 
267 inline void fromString(long long & destination, std::string_view value, int base) {
268  destination = Balau::Impl::stringToLongLong(value, base);
269 }
270 
283 inline void fromString(unsigned char & destination, std::string_view value) {
284  unsigned long n = Balau::Impl::stringToUnsignedLong(value);
285  destination = Balau::Impl::safeNumericCast<unsigned char>(n);
286 }
287 
301 inline void fromString(unsigned char & destination, std::string_view value, int base) {
302  unsigned long n = Balau::Impl::stringToUnsignedLong(value, base);
303  destination = Balau::Impl::safeNumericCast<unsigned char>(n);
304 }
305 
318 inline void fromString(unsigned short & destination, std::string_view value) {
319  unsigned long n = Balau::Impl::stringToUnsignedLong(value);
320  destination = Balau::Impl::safeNumericCast<unsigned short>(n);
321 }
322 
336 inline void fromString(unsigned short & destination, std::string_view value, int base) {
337  unsigned long n = Balau::Impl::stringToUnsignedLong(value, base);
338  destination = Balau::Impl::safeNumericCast<unsigned short>(n);
339 }
340 
353 inline void fromString(unsigned int & destination, std::string_view value) {
354  unsigned long n = Balau::Impl::stringToUnsignedLong(value);
355  destination = Balau::Impl::safeNumericCast<unsigned int>(n);
356 }
357 
371 inline void fromString(unsigned int & destination, std::string_view value, int base) {
372  unsigned long n = Balau::Impl::stringToUnsignedLong(value, base);
373  destination = Balau::Impl::safeNumericCast<unsigned int>(n);
374 }
375 
388 inline void fromString(unsigned long & destination, std::string_view value) {
389  destination = Balau::Impl::stringToUnsignedLong(value);
390 }
391 
405 inline void fromString(unsigned long & destination, std::string_view value, int base) {
406  destination = Balau::Impl::stringToUnsignedLong(value, base);
407 }
408 
421 inline void fromString(unsigned long long & destination, std::string_view value) {
422  destination = Balau::Impl::stringToUnsignedLongLong(value);
423 }
424 
438 inline void fromString(unsigned long long & destination, std::string_view value, int base) {
439  destination = Balau::Impl::stringToUnsignedLongLong(value, base);
440 }
441 
454 inline void fromString(float & destination, std::string_view value) {
455  destination = Balau::Impl::stringToFloat(value);
456 }
457 
470 inline void fromString(double & destination, std::string_view value) {
471  destination = Balau::Impl::stringToDouble(value);
472 }
473 
486 inline void fromString(long double & destination, std::string_view value) {
487  destination = Balau::Impl::stringToLongDouble(value);
488 }
489 
500 inline void fromString(bool & destination, std::string_view value) {
501  const std::string lower = Balau::Util::Strings::toLower(value);
502 
503  if (lower == "true") {
504  destination = true;
505  } else if (lower == "false") {
506  destination = false;
507  } else {
510  , toString("Cannot convert \"", value, "\" to boolean.")
511  );
512  }
513 }
514 
516 
523 inline void fromString16(std::string & destination, std::u16string_view value) {
524  destination = boost::locale::conv::utf_to_utf<char, char16_t>(std::u16string(value));
525 }
526 
533 inline void fromString16(std::u16string & destination, std::u16string_view value) {
534  destination = std::u16string(value.data(), value.length());
535 }
536 
543 inline void fromString16(std::u32string & destination, std::u16string_view value) {
544  destination = boost::locale::conv::utf_to_utf<char32_t, char16_t>(std::u16string(value));
545 }
546 
558 inline void fromString16(char & destination, std::u16string_view value) {
559  std::string valueStr = toString(value);
560  fromString(destination, valueStr);
561 }
562 
573 inline void fromString16(char32_t & destination, std::u16string_view value) {
574  std::string valueStr = toString(value);
575  fromString(destination, valueStr);
576 }
577 
590 inline void fromString16(signed char & destination, std::u16string_view value) {
591  std::string valueStr = toString(value);
592  long n = Balau::Impl::stringToLong(valueStr);
593  destination = Balau::Impl::safeNumericCast<signed char>(n);
594 }
595 
608 inline void fromString16(short & destination, std::u16string_view value) {
609  std::string valueStr = toString(value);
610  fromString(destination, valueStr);
611 }
612 
625 inline void fromString16(int & destination, std::u16string_view value) {
626  std::string valueStr = toString(value);
627  fromString(destination, valueStr);
628 }
629 
642 inline void fromString16(long & destination, std::u16string_view value) {
643  std::string valueStr = toString(value);
644  fromString(destination, valueStr);
645 }
646 
659 inline void fromString16(long long & destination, std::u16string_view value) {
660  std::string valueStr = toString(value);
661  fromString(destination, valueStr);
662 }
663 
676 inline void fromString16(unsigned char & destination, std::u16string_view value) {
677  std::string valueStr = toString(value);
678  fromString(destination, valueStr);
679 }
680 
693 inline void fromString16(unsigned short & destination, std::u16string_view value) {
694  std::string valueStr = toString(value);
695  fromString(destination, valueStr);
696 }
697 
710 inline void fromString16(unsigned int & destination, std::u16string_view value) {
711  std::string valueStr = toString(value);
712  fromString(destination, valueStr);
713 }
714 
727 inline void fromString16(unsigned long & destination, std::u16string_view value) {
728  std::string valueStr = toString(value);
729  fromString(destination, valueStr);
730 }
731 
744 inline void fromString16(unsigned long long & destination, std::u16string_view value) {
745  std::string valueStr = toString(value);
746  fromString(destination, valueStr);
747 }
748 
761 inline void fromString16(float & destination, std::u16string_view value) {
762  std::string valueStr = toString(value);
763  fromString(destination, valueStr);
764 }
765 
778 inline void fromString16(double & destination, std::u16string_view value) {
779  std::string valueStr = toString(value);
780  fromString(destination, valueStr);
781 }
782 
795 inline void fromString16(long double & destination, std::u16string_view value) {
796  std::string valueStr = toString(value);
797  fromString(destination, valueStr);
798 }
799 
810 inline void fromString16(bool & destination, std::u16string_view value) {
811  std::string valueStr = toString(value);
812  fromString(destination, valueStr);
813 }
814 
816 
823 inline void fromString32(std::string & destination, std::u32string_view value) {
824  destination = boost::locale::conv::utf_to_utf<char, char32_t>(std::u32string(value));
825 }
826 
833 inline void fromString32(std::u16string & destination, std::u32string_view value) {
834  destination = boost::locale::conv::utf_to_utf<char16_t, char32_t>(std::u32string(value));
835 }
836 
843 inline void fromString32(std::u32string & destination, std::u32string_view value) {
844  destination = std::u32string(value.data(), value.length());
845 }
846 
858 inline void fromString32(char & destination, std::u32string_view value) {
859  std::string valueStr = toString(value);
860  fromString(destination, valueStr);
861 }
862 
873 inline void fromString32(char32_t & destination, std::u32string_view value) {
874  Balau::Impl::validateFromString32Value(value);
875  destination = value[0];
876 }
877 
890 inline void fromString32(signed char & destination, std::u32string_view value) {
891  std::string valueStr = toString(value);
892  fromString(destination, valueStr);
893 }
894 
907 inline void fromString32(short & destination, std::u32string_view value) {
908  std::string valueStr = toString(value);
909  fromString(destination, valueStr);
910 }
911 
924 inline void fromString32(int & destination, std::u32string_view value) {
925  std::string valueStr = toString(value);
926  fromString(destination, valueStr);
927 }
928 
941 inline void fromString32(long & destination, std::u32string_view value) {
942  std::string valueStr = toString(value);
943  fromString(destination, valueStr);
944 }
945 
958 inline void fromString32(long long & destination, std::u32string_view value) {
959  std::string valueStr = toString(value);
960  fromString(destination, valueStr);
961 }
962 
975 inline void fromString32(unsigned char & destination, std::u32string_view value) {
976  std::string valueStr = toString(value);
977  fromString(destination, valueStr);
978 }
979 
992 inline void fromString32(unsigned short & destination, std::u32string_view value) {
993  std::string valueStr = toString(value);
994  fromString(destination, valueStr);
995 }
996 
1009 inline void fromString32(unsigned int & destination, std::u32string_view value) {
1010  std::string valueStr = toString(value);
1011  fromString(destination, valueStr);
1012 }
1013 
1026 inline void fromString32(unsigned long & destination, std::u32string_view value) {
1027  std::string valueStr = toString(value);
1028  fromString(destination, valueStr);
1029 }
1030 
1043 inline void fromString32(unsigned long long & destination, std::u32string_view value) {
1044  std::string valueStr = toString(value);
1045  fromString(destination, valueStr);
1046 }
1047 
1060 inline void fromString32(float & destination, std::u32string_view value) {
1061  std::string valueStr = toString(value);
1062  fromString(destination, valueStr);
1063 }
1064 
1077 inline void fromString32(double & destination, std::u32string_view value) {
1078  std::string valueStr = toString(value);
1079  fromString(destination, valueStr);
1080 }
1081 
1094 inline void fromString32(long double & destination, std::u32string_view value) {
1095  std::string valueStr = toString(value);
1096  fromString(destination, valueStr);
1097 }
1098 
1109 inline void fromString32(bool & destination, std::u32string_view value) {
1110  std::string valueStr = toString(value);
1111  fromString(destination, valueStr);
1112 }
1113 
1115 
1119 template <typename> struct FromString;
1120 
1126 template <> struct FromString<char> {
1132  template <typename T> void operator () (T & destination, std::string_view value) const {
1133  fromString(destination, value);
1134  }
1135 };
1136 
1142 template <> struct FromString<char16_t> {
1148  template <typename T> void operator () (T & destination, std::u16string_view value) const {
1149  fromString16(destination, value);
1150  }
1151 };
1152 
1158 template <> struct FromString<char32_t> {
1164  template <typename T> void operator () (T & destination, std::u32string_view value) const {
1165  fromString32(destination, value);
1166  }
1167 };
1168 
1169 #endif // COM_BORA_SOFTWARE__BALAU_APPLICATION__FROM_STRING
void fromString(std::string &destination, std::string_view value)
Sets the destination value to the supplied value.
Definition: FromString.hpp:30
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
Balau::U8String< AllocatorT > toString(const BalauException &e)
Base class toString<AllocatorT> function for Balau exceptions.
Definition: BalauException.hpp:122
static char32_t getNextUtf8Safe(const std::string_view &text, int &offset)
Get the next code point from the UTF-8 string view (validating version).
Definition: Character.hpp:198
Utilities for strings.
Thrown when a conversion fails.
Definition: BalauException.hpp:170
static std::string toLower(const std::string_view &s)
Convert the supplied UTF-8 string to lowercase.
Definition: Strings.hpp:457
Template class based from-string function for use in template classes.
Definition: FromString.hpp:1119
void fromString16(std::string &destination, std::u16string_view value)
Sets the destination value to a UTF-8 version of the supplied UTF-16 string.
Definition: FromString.hpp:523
void fromString32(std::string &destination, std::u32string_view value)
Sets the destination value to a UTF-8 version of the supplied UTF-32 string.
Definition: FromString.hpp:823