nds2-client - ClientDeveloper  0.16.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.hh
Go to the documentation of this file.
1 //
2 // Created by jonathan.hanks on 5/4/18.
3 //
4 
5 #ifndef NDS2_CLIENT_NDS1_COMMON_UTILS_HH
6 #define NDS2_CLIENT_NDS1_COMMON_UTILS_HH
7 
8 #include "common/utils.hh"
9 
10 namespace nds_impl
11 {
12  namespace nds1
13  {
14  namespace common
15  {
16 
17  template < typename It >
19  identify_padded_string( It begin, It end )
20  {
22  for ( It cur = begin; cur != end; ++cur, ++length )
23  {
24  if ( *cur == '\0' || *cur == ' ' )
25  {
26  break;
27  }
28  }
29  return nds_impl::common::Span< char >( &( *begin ), length );
30  }
31  }
32  }
33 }
34 
36 // Tests only after this point.
38 
39 #ifdef _NDS_IMPL_ENABLE_CATCH_TESTS_
40 
41 #include <vector>
42 #include <string>
43 
44 #include "common/utils.hh"
45 #include "catch.hpp"
46 
47 TEST_CASE( "There are several fields in NDS1 that are ' ' or 0 padded, given a "
48  "fixed length buffer, identify when the string ends",
49  "[nds1]" )
50 {
51 
52  std::vector< char > buf1{
53  '0', '1', '2', '3', '4', '5', ' ', ' ', ' ', ' '
54  };
55 
58  buf1.end( ) );
59  REQUIRE( span1.data( ) == buf1.data( ) );
60  REQUIRE( span1.size( ) == 6 );
61 
62  std::vector< char > buf2{ '0', '1', '2', '3', '4', '5', 0, 0, 0, 0 };
63 
66  buf2.end( ) );
67  REQUIRE( span2.data( ) == buf2.data( ) );
68  REQUIRE( span2.size( ) == 6 );
69 
70  std::vector< char > buf3{
71  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
72  };
73 
76  buf3.end( ) );
77  REQUIRE( span3.data( ) == buf3.data( ) );
78  REQUIRE( span3.size( ) == 10 );
79 
80  std::vector< char > buf4;
81  buf4.push_back( '0' ); // just make sure it is not empty
82 
85  buf4.begin( ) );
86  REQUIRE( span4.data( ) == buf4.data( ) );
87  REQUIRE( span4.size( ) == 0 );
88 }
89 
90 #endif // _NDS_IMPL_ENABLE_CATCH_TESTS_
91 
92 #endif // NDS2_CLIENT_NDS1_COMMON_UTILS_HH
TEST_CASE("daq_strlcpy copies strings safely when buffers are sufficiently large")
Definition: test_bsd_string.cc:9
nds_impl::common::Span< char > identify_padded_string(It begin, It end)
Definition: utils.hh:19
std::size_t size_type
Definition: utils.hh:139
pointer data() const
Definition: utils.hh:169
size_type size() const
Definition: utils.hh:154