GY-63_MS5611/libraries/Soundex/Soundex.cpp

116 lines
2.0 KiB
C++
Raw Normal View History

2022-02-06 10:08:32 -05:00
//
// FILE: Soundex.cpp
// AUTHOR: Rob Tillaart
2023-02-02 13:16:24 -05:00
// VERSION: 0.1.4
2022-02-06 10:08:32 -05:00
// DATE: 2022-02-05
// PURPOSE: Arduino Library for calculating Soundex hash
// URL: https://github.com/RobTillaart/Soundex
#include "Soundex.h"
Soundex::Soundex()
{
_buffer[0] = '\0';
_length = 4;
}
2022-02-07 08:46:24 -05:00
void Soundex::setLength(uint8_t length)
2022-02-06 10:08:32 -05:00
{
_length = length;
2022-02-07 08:46:24 -05:00
if (_length < SOUNDEX_MIN_LENGTH)
{
_length = SOUNDEX_MIN_LENGTH;
}
else if (_length > (SOUNDEX_MAX_LENGTH - 1))
2022-02-06 10:08:32 -05:00
{
_length = SOUNDEX_MAX_LENGTH - 1;
}
2023-02-02 13:16:24 -05:00
}
uint8_t Soundex::getLength()
{
return _length;
}
2022-02-06 10:08:32 -05:00
char * Soundex::soundex(const char * str)
{
uint8_t i = 0; // index for the buffer.
// fill buffer with zeros
for (i = 0; i < _length; i++) _buffer[i] = '0';
_buffer[_length] = '\0';
2022-11-24 14:21:52 -05:00
2022-02-06 10:08:32 -05:00
// find begin of word, skip spaces, digits
char *p = (char *) &str[0];
while((*p != 0) && (isalpha(*p) == false)) p++;
if (*p == 0) return _buffer;
// handle first character
i = 0;
_buffer[i++] = toupper(*p);
uint8_t last = sdx[_buffer[0] - 'A']; // remember last code
p++;
// process the remainder of the string
while ((*p != 0) && (i < _length))
{
if (isalpha(*p)) // skip non ASCII
{
uint8_t current = sdx[toupper(*p) - 'A'];
// new code?
if (last != current)
{
last = current;
if (last != 0) _buffer[i++] = '0' + last;
}
}
p++;
}
return _buffer;
}
2022-11-24 14:21:52 -05:00
// reference implementation
2022-02-07 08:46:24 -05:00
uint16_t Soundex::soundex16(const char * str)
{
uint8_t tmp = _length;
_length = 5;
char *p = soundex(str);
_length = tmp;
uint16_t value = (p[0] - 'A');
for (uint8_t i = 1; i < 5; i++)
{
value *= 7;
value += (p[i] - '0');
}
return value;
}
2022-11-24 14:21:52 -05:00
// reference implementation
2022-02-07 08:46:24 -05:00
uint32_t Soundex::soundex32(const char * str)
{
uint8_t tmp = _length;
_length = 10;
char *p = soundex(str);
_length = tmp;
uint32_t value = (p[0] - 'A');
for (uint8_t i = 1; i < 10; i++)
{
value *= 7;
value += (p[i] - '0');
}
return value;
}
2022-11-24 14:21:52 -05:00
// -- END OF FILE --
2022-02-06 10:08:32 -05:00