1 /*
2  * RESTinio
3  */
4 
5 /*!
6  * @file
7  * @brief Helpers for caseless comparison of strings.
8  *
9  * @since v.0.6.1
10  */
11 
12 #pragma once
13 
14 #include <restinio/impl/to_lower_lut.hpp>
15 #include <restinio/string_view.hpp>
16 
17 namespace restinio
18 {
19 
20 namespace impl
21 {
22 
23 namespace string_caseless_compare_details
24 {
25 
26 constexpr auto
uchar_at(const char * const from,const std::size_t at)27 uchar_at( const char * const from, const std::size_t at ) noexcept
28 {
29 	return static_cast< unsigned char >( from[ at ] );
30 }
31 
32 } /* namespace string_caseless_compare_details */
33 
34 //
35 // is_equal_caseless()
36 //
37 
38 //! Comparator for fields names.
39 inline bool
is_equal_caseless(const char * a,const char * b,std::size_t size)40 is_equal_caseless(
41 	const char * a,
42 	const char * b,
43 	std::size_t size ) noexcept
44 {
45 	using namespace string_caseless_compare_details;
46 
47 	const unsigned char * const table = to_lower_lut< unsigned char >();
48 
49 	for( std::size_t i = 0; i < size; ++i )
50 		if( table[ uchar_at( a, i ) ] != table[ uchar_at( b, i ) ] )
51 			return false;
52 
53 	return true;
54 }
55 
56 //
57 // is_equal_caseless()
58 //
59 
60 //! Comparator for fields names.
61 inline bool
is_equal_caseless(const char * a,std::size_t a_size,const char * b,std::size_t b_size)62 is_equal_caseless(
63 	const char * a,
64 	std::size_t a_size,
65 	const char * b,
66 	std::size_t b_size ) noexcept
67 {
68 	if( a_size == b_size )
69 	{
70 		return is_equal_caseless( a, b, a_size );
71 	}
72 
73 	return false;
74 }
75 
76 //
77 // is_equal_caseless()
78 //
79 
80 //! Comparator for fields names.
81 inline bool
is_equal_caseless(string_view_t a,string_view_t b)82 is_equal_caseless( string_view_t a, string_view_t b ) noexcept
83 {
84 	return is_equal_caseless( a.data(), a.size(), b.data(), b.size() );
85 }
86 
87 } /* namespace impl */
88 
89 } /* namespace restinio */
90 
91