nds2-client - ClientAdministrator  0.16.8
 All Functions Typedefs Enumerations Enumerator Groups Pages
utils.hh
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 >
18  nds_impl::common::Span< char >
19  identify_padded_string( It begin, It end )
20  {
21  nds_impl::common::Span< char >::size_type length = 0;
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 
56  nds_impl::common::Span< char > span1 =
57  nds_impl::nds1::common::identify_padded_string( buf1.begin( ),
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 
64  nds_impl::common::Span< char > span2 =
65  nds_impl::nds1::common::identify_padded_string( buf2.begin( ),
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 
74  nds_impl::common::Span< char > span3 =
75  nds_impl::nds1::common::identify_padded_string( buf3.begin( ),
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 
83  nds_impl::common::Span< char > span4 =
84  nds_impl::nds1::common::identify_padded_string( buf4.begin( ),
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