-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiff.cxx
32 lines (27 loc) · 982 Bytes
/
diff.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright 2017-2025 Mitchell. See LICENSE.
#include "diff_match_patch.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
/** diff() Lua function. */
static int diff(lua_State *L) {
diff_match_patch<std::string> dmp;
auto diffs = dmp.diff_main(luaL_checkstring(L, 1), luaL_checkstring(L, 2), false);
dmp.diff_cleanupSemantic(diffs);
lua_createtable(L, diffs.size() * 2, 0);
int len = 1;
for (auto &diff : diffs) {
lua_pushnumber(L, diff.operation), lua_rawseti(L, -2, len++);
lua_pushstring(L, diff.text.c_str()), lua_rawseti(L, -2, len++);
}
return 1;
}
extern "C" {
int luaopen_diff(lua_State *L) { return (lua_pushcfunction(L, diff), 1); }
// Platform-specific Lua library entry points.
LUALIB_API int luaopen_file_diff_diff(lua_State *L) { return luaopen_diff(L); }
LUALIB_API int luaopen_file_diff_diffosx(lua_State *L) { return luaopen_diff(L); }
LUALIB_API int luaopen_file_diff_diffarm(lua_State *L) { return luaopen_diff(L); }
}