Last active
September 11, 2020 18:40
-
-
Save jefgen/261424866469a1ad3ec6c35dd16a72e6 to your computer and use it in GitHub Desktop.
ICU-UStringSearch-Test for Pattern and Text inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple hacky test program | |
// | |
// Output: | |
// Hello. | |
// result: 7 | |
// result: 7 | |
// result: -1 | |
// result: -1 | |
// result: -1 | |
// done. | |
#include <iostream> | |
#include <Windows.h> | |
#include <icu.h> | |
int main() | |
{ | |
std::cout << "Hello.\n"; | |
UErrorCode status = U_ZERO_ERROR; | |
char patternString1[] = "abc"; | |
char patternString2[] = "abcdef"; | |
char textString1[] = "hello1 abcd world1"; | |
char textString2[] = "hello2 abcd world2"; | |
UChar* pattern1; | |
UChar* pattern2; | |
UChar* text1; | |
UChar* text2; | |
pattern1 = (UChar*)malloc(sizeof(UChar) * (strlen(patternString1) + 1)); | |
pattern2 = (UChar*)malloc(sizeof(UChar) * (strlen(patternString2) + 1)); | |
text1 = (UChar*)malloc(sizeof(UChar) * (strlen(textString1) + 1)); | |
text2 = (UChar*)malloc(sizeof(UChar) * (strlen(textString2) + 1)); | |
if (pattern1 == NULL || pattern2 == NULL || text1 == NULL || text2 == NULL) { | |
std::cout << "Error allocating memory.\n"; | |
free(pattern1); | |
free(pattern2); | |
free(text1); | |
free(text2); | |
return -1; | |
} | |
u_uastrcpy(pattern1, patternString1); | |
u_uastrcpy(pattern2, patternString2); | |
u_uastrcpy(text1, textString1); | |
u_uastrcpy(text2, textString2); | |
UStringSearch* search = usearch_open(pattern1, -1, text1, -1, "en", NULL, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error opening UStringSearch: " << u_errorName(status) << "\n"; | |
free(pattern1); | |
free(pattern2); | |
free(text1); | |
free(text2); | |
return -1; | |
} | |
int32_t result; | |
result = usearch_first(search, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
std::cout << "result: " << result << "\n"; // Output: 7 | |
// Free the pattern1 string. | |
memset(pattern1, 0, (sizeof(UChar) * (strlen(patternString1) + 1))); | |
free(pattern1); | |
pattern1 = NULL; | |
int32_t ptnLen = 0; | |
const UChar* ptn = usearch_getPattern(search, &ptnLen); | |
// This gives back a pointer to the free'd string. | |
// Search again, with a free'd pattern. | |
usearch_reset(search); | |
result = usearch_first(search, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
std::cout << "result: " << result << "\n"; // Output: 7 | |
// Free the text1 string. | |
memset(text1, 0, (sizeof(UChar) * (strlen(textString1) + 1))); | |
free(text1); | |
text1 = NULL; | |
int32_t txtLen = 0; | |
const UChar* txt = usearch_getText(search, &txtLen); | |
// This gives back a pointer to the free'd string. | |
// Search again, with a free'd pattern and text. | |
usearch_reset(search); | |
result = usearch_first(search, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
std::cout << "result: " << result << "\n"; // Output: -1 | |
// Change to use pattern2, even though text1 has been free'd. | |
usearch_setPattern(search, pattern2, -1, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
// Search again, with a valid pattern, but a free'd text. | |
usearch_reset(search); | |
result = usearch_first(search, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
std::cout << "result: " << result << "\n"; // Output: -1 | |
usearch_setText(search, text2, -1, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
usearch_reset(search); | |
result = usearch_first(search, &status); | |
if (U_FAILURE(status)) { | |
std::cout << "Error " << u_errorName(status) << "\n"; | |
} | |
std::cout << "result: " << result << "\n"; // Output: -1 | |
usearch_close(search); | |
free(pattern1); | |
free(pattern2); | |
free(text1); | |
free(text2); | |
std::cout << "done.\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment