Hi all,

I am looking for someone to help port enough of ICU so that a function to 
locale-sensitive string searching can be exposed to JS.

If you are interested, please contact me off-list, so we can discuss 
remuneration etc.

For clarity below is the C function (or similar) I want to expose to 
Javascript:

#include <stdlib.h>
#include <stdio.h>

#include <unicode/ucol.h>
#include <unicode/usearch.h>
#include <unicode/ustring.h>

void wrap_search(const char* utf8Needle, const char* utf8Haystack, const 
char* locale, void(*onResult)(int32_t, const UChar*)) {
  UErrorCode status = U_ZERO_ERROR;
 
  printf("about to call ucol_open");
  UCollator* col = ucol_open(locale, &status);
  if (U_FAILURE(status)) {
    printf("ucol_open Error code: %u\n", status);
    return;
  }

  printf("about to call ucol_setStrength");
  ucol_setStrength(col, UCOL_PRIMARY);

  int32_t needleLength;

  u_strFromUTF8(NULL, 0, &needleLength, utf8Needle, -1, &status);

  if (U_FAILURE(status)) {
    printf("u_strFromUTF8 (needle preflight) Error code: %u\n", status);
    return;
  }

  needleLength += 1;

  UChar* needle = (UChar*)malloc(needleLength * sizeof(UChar));

  u_strFromUTF8(needle, needleLength, &needleLength, utf8Needle, -1, &status
);

  if (U_FAILURE(status)) {
    printf("u_strFromUTF8(needle) Error code: %u\n", status);
    return;
  }

  int32_t haystackLength;

  u_strFromUTF8(NULL, 0, &haystackLength, utf8Haystack, -1, &status);

  if (U_FAILURE(status)) {
    printf("u_strFromUTF8 (haystack preflight) Error code: %u\n", status);
    return;
  }

  haystackLength += 1;

  UChar* haystack = (UChar*)malloc(haystackLength * sizeof(UChar));

  u_strFromUTF8(haystack, haystackLength, &haystackLength, utf8Haystack, -1, 
&status);

  if (U_FAILURE(status)) {
    printf("u_strFromUTF8(haystack) Error code: %u\n", status);
    return; 
  }

  printf("about to call usearch_openFromCollator");
  UStringSearch* searcher = usearch_openFromCollator(needle, -1, haystack, -
1, col, NULL, &status);
  if (U_FAILURE(status)) {
    printf("usearch_openFromCollator Error code: %u\n", status);
    ucol_close(col);
    return;
  }

  printf("about to call usearch_first");
  int32_t index = usearch_first(searcher, &status);
  if (U_FAILURE(status)) {
    printf("usearch_first Error code: %u\n", status);
    usearch_close(searcher);
    ucol_close(col);
    return;
  }
  while (index != USEARCH_DONE) {
    printf("about to call usearch_getMatchedLength");
    int32_t size = usearch_getMatchedLength(searcher) + 1;

    printf("about to call malloc");
    UChar* match = (UChar*)malloc(size * sizeof(UChar));
    if(!match) {
      printf("failed to allocate enough memory for the match");
      usearch_close(searcher);
      ucol_close(col);
      return;
    }

    printf("about to call usearch_getMatchedText");
    usearch_getMatchedText(searcher, match, size, &status);
    if (U_FAILURE(status)) {
      printf("usearch_getMatchedText Error code: %u\n", status);
      free(match);

      usearch_close(searcher);
      ucol_close(col);
      return;
    }

    printf("about to call onResult");
    printf("got result, index: %d, length: %d", index, size);
    // onResult(index, match);
    printf("about to call free");
    free(match);

    printf("about to call usearch_next");
    index = usearch_next(searcher, &status);
    if (U_FAILURE(status)) {
      printf("usearch_next Error code: %u\n", status);
      usearch_close(searcher);
      ucol_close(col);
      return;
    }
  }

  printf("about to call usearch_close");
  usearch_close(searcher);
  printf("about to call ucol_close");
  ucol_close(col);
}



Cheers,
Isaac

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to