<commit_before>#include "luaharfbuzz.h"

static int buffer_new(lua_State *L) {
  Buffer *b;


  b = (Buffer *)lua_newuserdata(L, sizeof(*b));
  luaL_getmetatable(L, "harfbuzz.Buffer");
  lua_setmetatable(L, -2);

  *b = hb_buffer_create();

   return 1;
}

static int buffer_guess_segment_properties(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  hb_buffer_guess_segment_properties(*b);

  return 0;
}

static int buffer_get_direction(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  lua_pushstring(L,hb_direction_to_string(hb_buffer_get_direction(*b)));

  return 1;
}

static int buffer_set_direction(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  const char* d = luaL_checkstring(L, 2);
  hb_direction_t dir  = hb_direction_from_string(d, strlen(d));

  if (dir == HB_DIRECTION_INVALID) {
    lua_pushstring(L, "Invalid direction");
    lua_error(L);
  }

  hb_buffer_set_direction(*b, dir);

  return 0;
}

static int buffer_get_language(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  lua_pushstring(L,hb_language_to_string(hb_buffer_get_language(*b)));

  return 1;
}

static int buffer_set_language(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  const char* l = luaL_checkstring(L, 2);
  hb_language_t lang  = hb_language_from_string(l, strlen(l));

  if (lang == HB_LANGUAGE_INVALID) {
    lua_pushstring(L, "Invalid language");
    lua_error(L);
  }

  hb_buffer_set_language(*b, lang);

  return 0;
}

static int buffer_get_script(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  char s[128];
  hb_tag_to_string(hb_script_to_iso15924_tag(hb_buffer_get_script(*b)),s);
  s[4] = '\0';
  lua_pushstring(L,s);

  return 1;
}

static int buffer_set_script(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  const char* s = luaL_checkstring(L, 2);
  hb_script_t script  = hb_script_from_string(s, strlen(s));

  if (script == HB_SCRIPT_UNKNOWN) {
    lua_pushstring(L, "Unknown script");
    lua_error(L);
  }

  hb_buffer_set_script(*b, script);

  return 0;
}

static int buffer_add_utf8(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  const char *text = luaL_checkstring(L, 2);

  hb_buffer_add_utf8(*b, text, strlen(text), 0, strlen(text));

  return 0;
}

static int buffer_destroy(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  hb_buffer_destroy(*b);

  return 0;
}

static int buffer_get_glyph_infos_and_positions(lua_State *L) {
  Buffer *buf = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  // Get glyph info and positions out of buffer
  unsigned int len = hb_buffer_get_length(*buf);
  hb_glyph_info_t *info = hb_buffer_get_glyph_infos(*buf, NULL);
  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(*buf, NULL);

  // Create Lua table and push glyph data onto it.
  lua_newtable(L); // parent table

  for (unsigned int i = 0; i < len; i++) {
    lua_pushnumber(L, i+1); // 1-indexed key parent table
    lua_newtable(L);        // child table

    lua_setfield_generic("codepoint", info[i].codepoint);
    lua_setfield_generic("cluster", info[i].cluster);
    lua_setfield_generic("x_advance", pos[i].x_advance);
    lua_setfield_generic("y_advance", pos[i].y_advance);
    lua_setfield_generic("x_offset", pos[i].x_offset);
    lua_setfield_generic("y_offset", pos[i].y_offset);

    lua_settable(L,-3); // Add child table at index i+1 to parent table
  }

  return 1;
}

static int buffer_reverse(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  hb_buffer_reverse(*b);

  return 0;
}

static const struct luaL_Reg buffer_methods[] = {
	{ "__gc", buffer_destroy },
  { "add_utf8", buffer_add_utf8 },
  { "set_direction", buffer_set_direction },
  { "get_direction", buffer_get_direction },
  { "set_language", buffer_set_language },
  { "get_language", buffer_get_language },
  { "set_script", buffer_set_script },
  { "get_script", buffer_get_script },
  { "get_glyph_infos_and_positions", buffer_get_glyph_infos_and_positions },
  { "guess_segment_properties", buffer_guess_segment_properties },
  { "reverse", buffer_reverse },
  { NULL, NULL },
};

static const struct luaL_Reg buffer_functions[] = {
  { "new", buffer_new },
  { NULL,  NULL }
};

int register_buffer(lua_State *L) {
  return register_class(L, "harfbuzz.Buffer", buffer_methods, buffer_functions);
}

<commit_msg>Minor fix: lua_pushnumber -> lua_pushinteger<commit_after>#include "luaharfbuzz.h"

static int buffer_new(lua_State *L) {
  Buffer *b;


  b = (Buffer *)lua_newuserdata(L, sizeof(*b));
  luaL_getmetatable(L, "harfbuzz.Buffer");
  lua_setmetatable(L, -2);

  *b = hb_buffer_create();

   return 1;
}

static int buffer_guess_segment_properties(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  hb_buffer_guess_segment_properties(*b);

  return 0;
}

static int buffer_get_direction(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  lua_pushstring(L,hb_direction_to_string(hb_buffer_get_direction(*b)));

  return 1;
}

static int buffer_set_direction(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  const char* d = luaL_checkstring(L, 2);
  hb_direction_t dir  = hb_direction_from_string(d, strlen(d));

  if (dir == HB_DIRECTION_INVALID) {
    lua_pushstring(L, "Invalid direction");
    lua_error(L);
  }

  hb_buffer_set_direction(*b, dir);

  return 0;
}

static int buffer_get_language(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  lua_pushstring(L,hb_language_to_string(hb_buffer_get_language(*b)));

  return 1;
}

static int buffer_set_language(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  const char* l = luaL_checkstring(L, 2);
  hb_language_t lang  = hb_language_from_string(l, strlen(l));

  if (lang == HB_LANGUAGE_INVALID) {
    lua_pushstring(L, "Invalid language");
    lua_error(L);
  }

  hb_buffer_set_language(*b, lang);

  return 0;
}

static int buffer_get_script(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  char s[128];
  hb_tag_to_string(hb_script_to_iso15924_tag(hb_buffer_get_script(*b)),s);
  s[4] = '\0';
  lua_pushstring(L,s);

  return 1;
}

static int buffer_set_script(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");
  const char* s = luaL_checkstring(L, 2);
  hb_script_t script  = hb_script_from_string(s, strlen(s));

  if (script == HB_SCRIPT_UNKNOWN) {
    lua_pushstring(L, "Unknown script");
    lua_error(L);
  }

  hb_buffer_set_script(*b, script);

  return 0;
}

static int buffer_add_utf8(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  const char *text = luaL_checkstring(L, 2);

  hb_buffer_add_utf8(*b, text, strlen(text), 0, strlen(text));

  return 0;
}

static int buffer_destroy(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  hb_buffer_destroy(*b);

  return 0;
}

static int buffer_get_glyph_infos_and_positions(lua_State *L) {
  Buffer *buf = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  // Get glyph info and positions out of buffer
  unsigned int len = hb_buffer_get_length(*buf);
  hb_glyph_info_t *info = hb_buffer_get_glyph_infos(*buf, NULL);
  hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(*buf, NULL);

  // Create Lua table and push glyph data onto it.
  lua_newtable(L); // parent table

  for (unsigned int i = 0; i < len; i++) {
    lua_pushinteger(L, i+1); // 1-indexed key parent table
    lua_newtable(L);        // child table

    lua_setfield_generic("codepoint", info[i].codepoint);
    lua_setfield_generic("cluster", info[i].cluster);
    lua_setfield_generic("x_advance", pos[i].x_advance);
    lua_setfield_generic("y_advance", pos[i].y_advance);
    lua_setfield_generic("x_offset", pos[i].x_offset);
    lua_setfield_generic("y_offset", pos[i].y_offset);

    lua_settable(L,-3); // Add child table at index i+1 to parent table
  }

  return 1;
}

static int buffer_reverse(lua_State *L) {
  Buffer *b = (Buffer *)luaL_checkudata(L, 1, "harfbuzz.Buffer");

  hb_buffer_reverse(*b);

  return 0;
}

static const struct luaL_Reg buffer_methods[] = {
	{ "__gc", buffer_destroy },
  { "add_utf8", buffer_add_utf8 },
  { "set_direction", buffer_set_direction },
  { "get_direction", buffer_get_direction },
  { "set_language", buffer_set_language },
  { "get_language", buffer_get_language },
  { "set_script", buffer_set_script },
  { "get_script", buffer_get_script },
  { "get_glyph_infos_and_positions", buffer_get_glyph_infos_and_positions },
  { "guess_segment_properties", buffer_guess_segment_properties },
  { "reverse", buffer_reverse },
  { NULL, NULL },
};

static const struct luaL_Reg buffer_functions[] = {
  { "new", buffer_new },
  { NULL,  NULL }
};

int register_buffer(lua_State *L) {
  return register_class(L, "harfbuzz.Buffer", buffer_methods, buffer_functions);
}


{'fraction_non_alphanumeric': 0.13973466724662897, 'fraction_numerical': 0.010439321444106133, 'mean_word_length': 5.042706964520368, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5076427', 'n_tokens_mistral': 3743, 'n_tokens_neox': 3511, 'n_words': 691}