MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / soundex

Method soundex

libraries/Soundex/Soundex.cpp:34–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32
33
34char * Soundex::soundex(const char * str)
35{
36 uint8_t i = 0; // index for the buffer.
37
38 // fill buffer with zeros
39 for (i = 0; i < _length; i++) _buffer[i] = '0';
40 _buffer[_length] = '\0';
41
42 // find begin of word, skip spaces, digits
43 char *p = (char *) &str[0];
44 while((*p != 0) && (isalpha(*p) == false)) p++;
45 if (*p == 0) return _buffer;
46
47 // handle first character
48 i = 0;
49 _buffer[i++] = toupper(*p);
50 uint8_t last = sdx[_buffer[0] - 'A']; // remember last code
51 p++;
52
53 // process the remainder of the string
54 while ((*p != 0) && (i < _length))
55 {
56 if (isalpha(*p)) // skip non ASCII
57 {
58 uint8_t current = sdx[toupper(*p) - 'A'];
59 // new code?
60 if (last != current)
61 {
62 last = current;
63 if (last != 0) _buffer[i++] = '0' + last;
64 }
65 }
66 p++;
67 }
68 return _buffer;
69}
70
71
72// reference implementation

Callers 1

unittestFunction · 0.80

Calls

no outgoing calls

Tested by 1

unittestFunction · 0.64