levenshtein.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Hartmut Holzgraefe <hholzgra@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_string.h"
  18. /* {{{ reference_levdist
  19. * reference implementation, only optimized for memory usage, not speed */
  20. static zend_long reference_levdist(const zend_string *string1, const zend_string *string2, zend_long cost_ins, zend_long cost_rep, zend_long cost_del )
  21. {
  22. zend_long *p1, *p2, *tmp;
  23. zend_long c0, c1, c2;
  24. size_t i1, i2;
  25. if (ZSTR_LEN(string1) == 0) {
  26. return ZSTR_LEN(string2) * cost_ins;
  27. }
  28. if (ZSTR_LEN(string2) == 0) {
  29. return ZSTR_LEN(string1) * cost_del;
  30. }
  31. p1 = safe_emalloc((ZSTR_LEN(string2) + 1), sizeof(zend_long), 0);
  32. p2 = safe_emalloc((ZSTR_LEN(string2) + 1), sizeof(zend_long), 0);
  33. for (i2 = 0; i2 <= ZSTR_LEN(string2); i2++) {
  34. p1[i2] = i2 * cost_ins;
  35. }
  36. for (i1 = 0; i1 < ZSTR_LEN(string1) ; i1++) {
  37. p2[0] = p1[0] + cost_del;
  38. for (i2 = 0; i2 < ZSTR_LEN(string2); i2++) {
  39. c0 = p1[i2] + ((ZSTR_VAL(string1)[i1] == ZSTR_VAL(string2)[i2]) ? 0 : cost_rep);
  40. c1 = p1[i2 + 1] + cost_del;
  41. if (c1 < c0) {
  42. c0 = c1;
  43. }
  44. c2 = p2[i2] + cost_ins;
  45. if (c2 < c0) {
  46. c0 = c2;
  47. }
  48. p2[i2 + 1] = c0;
  49. }
  50. tmp = p1;
  51. p1 = p2;
  52. p2 = tmp;
  53. }
  54. c0 = p1[ZSTR_LEN(string2)];
  55. efree(p1);
  56. efree(p2);
  57. return c0;
  58. }
  59. /* }}} */
  60. /* {{{ Calculate Levenshtein distance between two strings */
  61. PHP_FUNCTION(levenshtein)
  62. {
  63. zend_string *string1, *string2;
  64. zend_long cost_ins = 1;
  65. zend_long cost_rep = 1;
  66. zend_long cost_del = 1;
  67. if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lll", &string1, &string2, &cost_ins, &cost_rep, &cost_del) == FAILURE) {
  68. RETURN_THROWS();
  69. }
  70. RETURN_LONG(reference_levdist(string1, string2, cost_ins, cost_rep, cost_del));
  71. }
  72. /* }}} */