Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. -- List functions
  2. --
  3. --
  4. LIST_DELIM = "~"
  5.  
  6. function list_add(key, value)
  7. --- Determine the limit by inspecting if | is in the key
  8. local limit = 200
  9.  
  10. if string.find(key, '|') then
  11. local key_parts = _tokenize(key, '|')
  12. limit = tonumber(key_parts[1])
  13. key = key_parts[2]
  14. end
  15.  
  16. local current_value = _get(key) or ''
  17.  
  18. --- Filter out values so we don't insert duplicates
  19. local search_val = LIST_DELIM .. current_value
  20. local values = _tokenize(value, LIST_DELIM)
  21. local filtered = {}
  22.  
  23. for i=1, #values do
  24. if not string.match(search_val, LIST_DELIM .. values[i] .. LIST_DELIM) then
  25. table.insert(filtered, values[i])
  26. end
  27. end
  28.  
  29. --- Check if they are all duplicates
  30. if #filtered == 0 then
  31. return "ok"
  32. end
  33.  
  34. values = filtered
  35.  
  36. --- Check if we should apply cleanup
  37. local list_size = char_count(search_val, LIST_DELIM)
  38. list_size = list_size + #values
  39.  
  40. if list_size > (limit + 100) then
  41. local current_list = _current_list(current_value, nil)
  42. _list_cleanup(key, current_list, limit, #values)
  43. end
  44.  
  45. --- Append values at the end of the list
  46. _putcat(key, value)
  47. return "ok"
  48. end
  49.  
  50.  
  51. function list_remove(key, value)
  52. local current_list = _get(key)
  53.  
  54. if not current_list then
  55. return "ok"
  56. end
  57.  
  58. local tokens = _tokenize(value, LIST_DELIM)
  59.  
  60. for i = 1, #tokens do
  61. current_list = string.gsub(current_list, tokens[i] .. LIST_DELIM, "")
  62. end
  63.  
  64. _put(key, current_list)
  65.  
  66. return "ok"
  67. end
  68.  
  69.  
  70.  
  71. --
  72. -- Helpers
  73. --
  74. function _tokenize(text, delims)
  75. local tokens = {}
  76. for token in string.gmatch(text, "[^" .. delims .. "]+") do
  77. if #token > 0 then
  78. table.insert(tokens, token)
  79. end
  80. end
  81. return tokens
  82. end
  83.  
  84. function _current_list(old_list, tokens)
  85. if old_list then
  86. if tokens then
  87. for i = 1, #tokens do
  88. old_list = string.gsub(old_list, "^" .. tokens[i] .. LIST_DELIM, "")
  89. old_list = string.gsub(old_list, LIST_DELIM .. tokens[i] .. LIST_DELIM, LIST_DELIM)
  90. end
  91. end
  92.  
  93. return _tokenize(old_list, LIST_DELIM)
  94. else
  95. return {}
  96. end
  97. end
  98.  
  99.  
  100. --- Deletes the first entries in the list
  101. --- so there's only `limit` entires left in the list
  102. function _list_cleanup(key, current_list, limit, new_items_size)
  103. local current_size = #current_list + new_items_size
  104.  
  105. while current_size > limit do
  106. --remove the first item
  107. table.remove(current_list, 1)
  108. current_size = current_size - 1
  109. end
  110.  
  111. _list_store(key, current_list)
  112.  
  113. return current_size
  114. end
  115.  
  116. function _list_store(key, list)
  117. local result = table.concat(list, LIST_DELIM) .. LIST_DELIM
  118. _put(key, result)
  119. end
  120.  
  121. function char_count(str, char)
  122. if not str then
  123. return 0
  124. end
  125.  
  126. local count = 0
  127. local byte_char = string.byte(char)
  128. for i = 1, #str do
  129. if string.byte(str, i) == byte_char then
  130. count = count + 1
  131. end
  132. end
  133. return count
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement