utils_binding.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h> // Add this line for std::vector conversion
  3. #include "utils.h"
  4. namespace py = pybind11;
  5. PYBIND11_MODULE(c_utils, m) {
  6. m.doc() = "C implementations of string utility functions";
  7. m.def(
  8. "find_common_prefix",
  9. [](const std::string& s1, const std::string& s2) {
  10. char* result = find_common_prefix(s1.c_str(), s2.c_str());
  11. std::string py_result(result);
  12. free(result);
  13. return py_result;
  14. },
  15. "Find common prefix between two strings");
  16. m.def(
  17. "find_common_suffix",
  18. [](const std::string& s1, const std::string& s2) {
  19. char* result = find_common_suffix(s1.c_str(), s2.c_str());
  20. std::string py_result(result);
  21. free(result);
  22. return py_result;
  23. },
  24. "Find common suffix between two strings");
  25. m.def(
  26. "extract_intermediate_diff",
  27. [](const std::string& curr, const std::string& old) {
  28. char* result = extract_intermediate_diff(curr.c_str(), old.c_str());
  29. std::string py_result(result);
  30. free(result);
  31. return py_result;
  32. },
  33. "Extract intermediate difference between two strings");
  34. m.def(
  35. "find_all_indices",
  36. [](const std::string& string, const std::string& substring) {
  37. int count;
  38. int* indices =
  39. find_all_indices(string.c_str(), substring.c_str(), &count);
  40. std::vector<int> result(indices, indices + count);
  41. free(indices);
  42. return result;
  43. },
  44. "Find all indices of substring in string");
  45. }