1+ /*
2+ *********************************************************************************************************
3+ * INCLUDE FILES
4+ *********************************************************************************************************
5+ */
6+
7+ #include "string_matching.h"
8+
9+ /*
10+ *********************************************************************************************************
11+ * LOCAL DEFINES
12+ *********************************************************************************************************
13+ */
14+
15+ /*
16+ *********************************************************************************************************
17+ * LOCAL CONSTANTS
18+ *********************************************************************************************************
19+ */
20+
21+ /*
22+ *********************************************************************************************************
23+ * LOCAL DATA TYPES
24+ *********************************************************************************************************
25+ */
26+
27+ /*
28+ *********************************************************************************************************
29+ * LOCAL TABLES
30+ *********************************************************************************************************
31+ */
32+
33+ /*
34+ *********************************************************************************************************
35+ * LOCAL GLOBAL VARIABLES
36+ *********************************************************************************************************
37+ */
38+
39+ /*
40+ *********************************************************************************************************
41+ * LOCAL FUNCTION PROTOTYPES
42+ *********************************************************************************************************
43+ */
44+
45+ /*
46+ *********************************************************************************************************
47+ * FUNCTIONS
48+ *********************************************************************************************************
49+ */
50+
51+ /**
52+ * @brief This function will search the substring upon the string by the sunday algorithm.
53+ *
54+ * @param str the pointer to the string.
55+ * @param substr the pointer to the substring.
56+ * @param len the length of the substring.
57+ *
58+ * @return the max matching rate.
59+ */
60+
61+ float substring_search_control_sunday_algorithm (const char * str , size_t len ,
62+ const char * substr , size_t sublen )
63+ {
64+ assert (str );
65+ assert (len );
66+ assert (substr );
67+ assert (sublen );
68+
69+ static size_t
70+ loc , step , subloc ;
71+
72+ static char
73+ symbol_substr ;
74+
75+ static float
76+ max_matching_rate ;
77+
78+ struct sunday_algorith_substring_infomation_s {
79+ bool exist ;
80+
81+ size_t location ;
82+ }symbol [128 ] = { 0 }; /* ASSIC have 128 symbol */
83+
84+ loc = 0 ; /* Set zero */
85+ step = 0 ;
86+ subloc = 0 ;
87+ max_matching_rate = 0.0 ;
88+
89+ do { /* Get the information of the substring,
90+ whether the symbol exist and where it is */
91+ symbol_substr = * (substr + subloc );
92+
93+ if (false == symbol [symbol_substr ].exist ) {
94+ symbol [symbol_substr ].exist = true;
95+ }
96+
97+ symbol [symbol_substr ].location = sublen - subloc - 1 ;
98+ } while (subloc ++ < sublen );
99+
100+ do {
101+ subloc = 0 ;
102+ do {
103+ symbol_substr = * (str + loc + subloc );
104+
105+ if (* (substr + subloc ) != symbol_substr ) {
106+ #if (STRING_MATCHING_CFG_DEBUG_EN )
107+
108+ printf ("substring_search.sunday_algorithm.not match:str \'%c\' - substr \'%c\' the point is \'%c\' \r\n" ,
109+ * (substr + subloc ), symbol_substr , * (str + loc + sublen ));
110+
111+ #endif // (STRING_MATCHING_CFG_DEBUG_EN)
112+
113+ symbol_substr = * (str + loc + sublen );
114+
115+ if (true == symbol [symbol_substr ].exist ) {
116+ #if (STRING_MATCHING_CFG_DEBUG_EN )
117+
118+ printf ("substring_search.sunday_algorithm.not match.point is exist \r\n" );
119+
120+ #endif // (STRING_MATCHING_CFG_DEBUG_EN)
121+
122+ step = symbol [symbol_substr ].location + 1 ;
123+ } else {
124+ #if (STRING_MATCHING_CFG_DEBUG_EN )
125+
126+ printf ("substring_search.sunday_algorithm.not match.point is not exist \r\n" );
127+
128+ #endif // (STRING_MATCHING_CFG_DEBUG_EN)
129+
130+ step = sublen + 1 ;
131+ }
132+
133+ #if (STRING_MATCHING_CFG_DEBUG_EN )
134+
135+ printf ("substring_search.sunday_algorithm.step:%d \r\n" , step );
136+
137+ #endif // (STRING_MATCHING_CFG_DEBUG_EN)
138+
139+ if (subloc && max_matching_rate < subloc / (float )sublen ) {
140+ max_matching_rate = subloc / (float )sublen ;
141+ }
142+
143+ break ;
144+ } else if (sublen - 1 == subloc ) {
145+ return 1.0 ;
146+ }
147+ } while (subloc ++ < sublen );
148+ } while ((loc += step ) < len );
149+
150+ return max_matching_rate ;
151+ }
0 commit comments