hipack-misc.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * hipack-misc.c
  3. * Copyright (C) 2015 Adrian Perez <aperez@igalia.com>
  4. *
  5. * Distributed under terms of the MIT license.
  6. */
  7. #include "hipack.h"
  8. #include <math.h>
  9. bool
  10. hipack_value_equal (const hipack_value_t *a,
  11. const hipack_value_t *b)
  12. {
  13. assert (a);
  14. assert (b);
  15. if (a->type != b->type)
  16. return false;
  17. switch (a->type) {
  18. case HIPACK_INTEGER:
  19. return a->v_integer == b->v_integer;
  20. case HIPACK_BOOL:
  21. return a->v_bool == b->v_bool;
  22. case HIPACK_FLOAT:
  23. return fabs(b->v_float - a->v_float) < 1e-15;
  24. case HIPACK_STRING:
  25. return hipack_string_equal (a->v_string, b->v_string);
  26. case HIPACK_LIST:
  27. return hipack_list_equal (a->v_list, b->v_list);
  28. case HIPACK_DICT:
  29. return hipack_dict_equal (a->v_dict, b->v_dict);
  30. default:
  31. assert (false); // Unreachable.
  32. }
  33. }
  34. bool
  35. hipack_list_equal (const hipack_list_t *a,
  36. const hipack_list_t *b)
  37. {
  38. assert (a);
  39. assert (b);
  40. if (a->size != b->size)
  41. return false;
  42. for (uint32_t i = 0; i < a->size; i++)
  43. if (!hipack_value_equal (&a->data[i], &b->data[i]))
  44. return false;
  45. return true;
  46. }
  47. bool
  48. hipack_dict_equal (const hipack_dict_t *a,
  49. const hipack_dict_t *b)
  50. {
  51. assert (a);
  52. assert (b);
  53. if (a->count != b->count)
  54. return false;
  55. const hipack_string_t *key;
  56. hipack_value_t *a_value;
  57. HIPACK_DICT_FOREACH (a, key, a_value) {
  58. hipack_value_t *b_value = hipack_dict_get (b, key);
  59. if (!b_value || !hipack_value_equal (a_value, b_value)) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }