LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - string_util.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 60 68 88.2 %
Date: 2026-11-09 02:10:49 Functions: 29 33 87.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #ifndef BASE_STRING_UTIL_H__
       7             : #define BASE_STRING_UTIL_H__
       8             : 
       9             : #include <string>
      10             : #include <sstream>
      11             : #include <vector>
      12             : 
      13             : #include <boost/algorithm/string.hpp>
      14             : #include <boost/uuid/nil_generator.hpp>
      15             : #include <boost/uuid/uuid.hpp>
      16             : #include <boost/uuid/uuid_io.hpp>
      17             : 
      18             : // Writes a number into a string
      19             : template <typename NumberType>
      20    18794698 : static inline const std::string integerToString(const NumberType &num) {
      21    18794698 :     std::stringstream ss;
      22    18795482 :     ss << num;
      23    37590515 :     return ss.str();
      24    18795424 : }
      25             : 
      26             : // int8_t must be handled specially because std::stringstream sees
      27             : // int8_t as a text type instead of an integer type
      28             : template <>
      29           0 : inline const std::string integerToString<>(const int8_t &num) {
      30           0 :     std::stringstream ss;
      31           0 :     ss << (int16_t)num;
      32           0 :     return ss.str();
      33           0 : }
      34             : 
      35             : // uint8_t must be handled specially because std::stringstream sees
      36             : // uint8_t as a text type instead of an integer type
      37             : template <>
      38     1182028 : inline const std::string integerToString<>(const uint8_t &num) {
      39     1182028 :     std::stringstream ss;
      40     1182033 :     ss << (uint16_t)num;
      41     2364064 :     return ss.str();
      42     1182033 : }
      43             : 
      44             : // Writes a number into a string as hex
      45             : template <typename NumberType>
      46       40851 : static inline const std::string integerToHexString(const NumberType &num) {
      47       40851 :     std::stringstream ss;
      48       40851 :     ss << std::hex << num;
      49       81702 :     return ss.str();
      50       40851 : }
      51             : 
      52             : // signed int8 must be handled specially because std::stringstream sees
      53             : // int8_t as a text type instead of an integer type
      54             : template <>
      55             : inline const std::string integerToHexString<>(const int8_t &num) {
      56             :     std::stringstream ss;
      57             :     ss << std::hex << (int16_t)num;
      58             :     return ss.str();
      59             : }
      60             : 
      61             : // unsigned int8 must be handled specially because std::stringstream sees
      62             : // u_int8_t as a text type instead of an integer type
      63             : template <>
      64             : inline const std::string integerToHexString<>(const uint8_t &num) {
      65             :     std::stringstream ss;
      66             :     ss << std::hex << (uint16_t)num;
      67             :     return ss.str();
      68             : }
      69             : 
      70             : // Converts string into a number
      71             : template <typename NumberType>
      72      262714 : inline bool stringToInteger(const std::string& str, NumberType &num) {
      73             :     char *endptr;
      74      262714 :     num = strtoul(str.c_str(), &endptr, 10);
      75      262714 :     return endptr[0] == '\0';
      76             : }
      77             : 
      78             : template <typename NumberType>
      79       17832 : inline bool stringToLongLong(const std::string& str, NumberType &num) {
      80             :     char *endptr;
      81       17832 :     num = strtoull(str.c_str(), &endptr, 10);
      82       17834 :     return endptr[0] == '\0';
      83             : }
      84             : 
      85             : template <>
      86         123 : inline bool stringToInteger<>(const std::string& str, int64_t &num) {
      87         123 :     return stringToLongLong(str, num);
      88             : }
      89             : 
      90             : template <>
      91       17707 : inline bool stringToInteger<>(const std::string& str, uint64_t &num) {
      92       17707 :     return stringToLongLong(str, num);
      93             : }
      94             : 
      95             : template <>
      96           8 : inline bool stringToInteger<>(const std::string& str, double &num) {
      97             :     char *endptr;
      98           8 :     num = strtod(str.c_str(), &endptr);
      99           8 :     return endptr[0] == '\0';
     100             : }
     101             : 
     102             : //
     103             : // Split a the initial part of the string based on 'seperator' characters
     104             : // and return the resultant list of tokens after converting each token into
     105             : // NumberType elements
     106             : //
     107             : template<typename NumberType>
     108         729 : static inline bool stringToIntegerList(std::string input,
     109             :                                        std::string seperator,
     110             :                                        std::vector<NumberType> &entries) {
     111         729 :     std::vector<std::string> tokens;
     112             : 
     113         729 :     boost::split(tokens, input, boost::is_any_of(seperator),
     114             :                  boost::token_compress_on);
     115             : 
     116         729 :     if (!tokens.size()) {
     117           0 :         return false;
     118             :     }
     119             : 
     120         729 :     std::vector<std::string>::iterator iter;
     121             : 
     122        3641 :     for (iter = tokens.begin(); iter != tokens.end(); iter++) {
     123        1457 :         std::stringstream ss(*iter);
     124             :         NumberType value;
     125        1457 :         ss >> value;
     126             : 
     127             :         //
     128             :         // Bail if there is an error during the conversion.
     129             :         //
     130        1457 :         if (ss.fail()) {
     131           0 :             return false;
     132             :         }
     133        1457 :         entries.push_back(value);
     134             :     }
     135             : 
     136         728 :     return true;
     137         728 : }
     138             : 
     139       14640 : static inline std::string UuidToString(const boost::uuids::uuid &id)
     140             : {
     141       14640 :     std::stringstream uuidstring;
     142       14640 :     uuidstring << id;
     143       29280 :     return uuidstring.str();
     144       14640 : }
     145             : 
     146       31452 : static inline boost::uuids::uuid StringToUuid(const std::string &str)
     147             : {
     148       31452 :     boost::uuids::uuid u = boost::uuids::nil_uuid();
     149       31452 :     std::stringstream uuidstring(str);
     150       31452 :     uuidstring >> u;
     151       31452 :     return u;
     152       31452 : }
     153             : 
     154          26 : static inline std::string BoolToString(const bool &val) {
     155          26 :     if (val) {
     156           1 :         return "true";
     157             :     } else {
     158          25 :         return "false";
     159             :     }
     160             : }
     161             : 
     162           2 : static inline bool StringToBool(const std::string &in_string) {
     163           2 :     if (in_string.compare("true") == 0) {
     164           1 :         return true;
     165             :     }
     166           1 :     if (in_string.compare("false") == 0) {
     167           1 :         return false;
     168             :     }
     169           0 :     return false;
     170             : }
     171             : 
     172             : #endif  // BASE_STRING_UTIL_H__

Generated by: LCOV version 1.14