Design Pattern Examples
Overview of object-oriented design patterns
wcsstri.cpp
Go to the documentation of this file.
1
5
6#include <wctype.h>
7#include "wcsstri.h"
8
9namespace Helpers
10{
12 //
13 // Function : wcsstri
14 //
15 // Scope : Public
16 //
17 // Arguments : str1 = pointer to string to search in
18 // str2 = pointer to string to search for
19 //
20 // Returns : Pointer into str1 to first character of found str2 or
21 // nullptr if str2 is not found in str1.
22 //
23 // Description: Find string str2 in string str1, returning pointer to first
24 // occurrence of str2. Returns nullptr if str2 not found in str1.
25 //
26 // Notes : This is functionally identical to the C Library's wcsstr()
27 // except for the use of towupper().
28 //
30 wchar_t* wcsstri(
31 const wchar_t* s1,
32 const wchar_t* s2)
33 {
34 // Guard against null pointers (wcsstr() does not do this).
35 if (s1 == nullptr || s2 == nullptr)
36 {
37 return (nullptr);
38 }
39
40 // If string to search for is empty, return beginning of str1.
41 if (*s2 == L'\0')
42 {
43 return ((wchar_t*)s1);
44 }
45
46 // Loop over all characters in string to search
47 while (*s1 != L'\0')
48 {
49 // Find first occurrence of second string in string 1
50 while (*s1 != L'\0' && towupper(*s1) != towupper(*s2))
51 {
52 s1++;
53 }
54
55 if (*s1 == L'\0')
56 {
57 break;
58 }
59 const wchar_t* sc1;
60 const wchar_t* sc2;
61
62 // Now that the first character of string 2 might have been
63 // found, compare subsequent characters of string 2 to see
64 // if we have an exact match.
65 for (sc1 = s1, sc2 = s2; ; )
66 {
67 if (*++sc2 == L'\0')
68 {
69 // We have reached the end of the string to search for
70 // so we must have a match. Return pointer into s1
71 // to the found string.
72 return ((wchar_t*)s1);
73 }
74 else
75 {
76 ++sc1;
77
78 if (towupper(*sc1) != towupper(*sc2))
79 {
80 // a character in string 2 doesn't match so start
81 // the whole thing over with the next character
82 // in string 1.
83 break;
84 }
85 }
86 }
87 ++s1;
88 }
89 // If we get here, there is no match so return nullptr.
90 return (nullptr);
91 }
92} // end namespace
The namespace containing all the "helper" functions in the C++ code.
wchar_t * wcsstri(const wchar_t *s1, const wchar_t *s2)
Do case-insensitive search for string 2 (s2) in string 1 (s1). Similar to the C library's wcsstr().
Definition: wcsstri.cpp:30
Declaration of the wcsstri function, case-insensitive string search for wide character strings.