LCOV - code coverage report
Current view: top level - libs/common - byteutils.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 8 10 80.0 %
Date: 2019-03-07 14:46:43 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_BYTEUTILS_H
       7             : #define IROHA_BYTEUTILS_H
       8             : 
       9             : #include <algorithm>
      10             : #include <string>
      11             : #include <vector>
      12             : 
      13             : #include <boost/optional.hpp>
      14             : 
      15             : #include "common/bind.hpp"
      16             : #include "common/blob.hpp"
      17             : #include "common/hexutils.hpp"
      18             : 
      19             : namespace iroha {
      20             :   /**
      21             :    * Convert string to blob vector
      22             :    * @param source - string for conversion
      23             :    * @return vector<blob>
      24             :    */
      25             :   inline std::vector<uint8_t> stringToBytes(const std::string &source) {
      26        1294 :     return std::vector<uint8_t>(source.begin(), source.end());
      27             :   }
      28             : 
      29             :   /**
      30             :    * blob vector to string
      31             :    * @param source - vector for conversion
      32             :    * @return result string
      33             :    */
      34             :   inline std::string bytesToString(const std::vector<uint8_t> &source) {
      35        1577 :     return std::string(source.begin(), source.end());
      36             :   }
      37             : 
      38             :   /**
      39             :    * Create blob_t from string of specified size
      40             :    * @tparam size - size of blob_t, expected size of string
      41             :    * @param s - string to convert
      42             :    * @return blob, if conversion was successful, otherwise nullopt
      43             :    */
      44             :   template <size_t size>
      45             :   boost::optional<blob_t<size>> stringToBlob(const std::string &string) {
      46          47 :     if (size != string.size()) {
      47           0 :       return boost::none;
      48             :     }
      49          47 :     blob_t<size> array;
      50          47 :     std::copy(string.begin(), string.end(), array.begin());
      51          47 :     return array;
      52          47 :   }
      53             : 
      54             :   /**
      55             :    * Convert hexstring to array of given size
      56             :    * @tparam size - output array size
      57             :    * @param string - input string for transform
      58             :    * @return array of given size if size matches, nullopt otherwise
      59             :    */
      60             :   template <size_t size>
      61             :   boost::optional<blob_t<size>> hexstringToArray(const std::string &string) {
      62          48 :     return hexstringToBytestring(string) | stringToBlob<size>;
      63           0 :   }
      64             : 
      65             : }  // namespace iroha
      66             : 
      67             : #endif  // IROHA_BYTEUTILS_H

Generated by: LCOV version 1.13