{"id":19599702,"url":"https://github.com/starwing/lbuffer","last_synced_at":"2025-04-27T16:32:07.544Z","repository":{"id":137296999,"uuid":"2536312","full_name":"starwing/lbuffer","owner":"starwing","description":"a mutable string support to lua.","archived":false,"fork":false,"pushed_at":"2015-03-20T19:33:19.000Z","size":261,"stargazers_count":25,"open_issues_count":4,"forks_count":7,"subscribers_count":4,"default_branch":"buff_interface","last_synced_at":"2025-04-05T01:32:08.759Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/starwing.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-10-08T01:36:21.000Z","updated_at":"2024-12-13T00:02:06.000Z","dependencies_parsed_at":"2023-03-14T02:46:22.475Z","dependency_job_id":null,"html_url":"https://github.com/starwing/lbuffer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flbuffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flbuffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flbuffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/starwing%2Flbuffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/starwing","download_url":"https://codeload.github.com/starwing/lbuffer/tar.gz/refs/heads/buff_interface","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251171447,"owners_count":21547112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T09:12:12.599Z","updated_at":"2025-04-27T16:32:07.298Z","avatar_url":"https://github.com/starwing.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"lbuffer - mutable string support to lua\n=========================================\n\nlbuffer is a C module for lua, it provides mutable string feature to\nthe lua_ language. it has all routines from lua's string module, add\nseveral modify functions. it provides:\n\n    * change the value of buffer without copy it.\n    * a pair of full feature pack/unpack functions.\n    * get a subbuffer from the original buffer, and the changes to\n      subbufer will affects the original buffer.\n\nand you can add ``lbuffer.h`` and recompile the other C module to get\nfull lbuffer compatible with other lua module. just add: ::\n\n    -DLB_REPLACE_LUA_API -include lbuffer.h\n\nwhen you compile other Lua C modules (using gcc). e.g. if you compile\na lbuffer compatible md5_ module, you can use them together like\nthis: ::\n\n    require 'buffer'\n    require 'md5'\n    print(md5.sumhexa(buffer \"hello world\"))\n\n\n.. _lua: http://www.lua.org\n.. _md5: https://github.com/keplerproject/md5\n\n\ninstall\n=======\n\ncompile it just like other lua module. in ``*nix``, just: ::\n\n    gcc -shared -obuffer.so *.c -llua51\n\nand in Windows using MinGW, just: ::\n\n    gcc -mdll -DLUA_BUILD_AS_DLL -I/path/to/lua/include *.c /path/to/lua51.dll -o buffer.dll\n\nand in Windows using ``MSVC``, just create a Win32/DLL Project, and\nadd all .c and .h files to project, set output name to ``buffer.dll``\nand compile it.\n\nif you want subbuffer feature, you need define a macro named\n``LB_SUBBUFFER``, because subbuffer will slow the memory realloc\nfunction in **all** buffers.\n\nthere are two method to do ``pack``/``unpack`` operations. defaultly\nwe read soem bits to a buffer, and cast it to int, and do bit swap.\nbut you can also choose bit-op ways to extract binary numbers in file,\nthis can be used in machines that has any number of bit in a byte, but\nthis may somehow slow a bit. define ``LB_ARTHBIT`` to enable this.\n\nexample\n=======\n\nthere are some examples, and main usage please see test.lua.\n\nfirst, you can use lbuffer just like using a normal string, you can\ncall string functions on it, but the functions are in lbuffer module of\ncourse: ::\n\n        local B = require 'buffer'\n        local b = B'hello'\n        print(#b)\n        print(b:upper())\n        print(b:reverse())\n\nthis will output: ::\n\n    5\n    HELLO\n    OLLEH\n\nnotice that all these functions don't create a new buffer/string,\ninstead, it changes the content of operated buffer. so the output of\nthe last line is ``OLLEH``, but not ``olleh``.\n\nbeside the normal functions inherit from the standard string_ module,\nthere are also many functions that only in lbuffer module, see\nreference_ of buffer to know these special functions, there are some\nexamples: ::\n    \n    $ lua -lbuffer\n    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio\n    \u003e b = buffer \"abc\"\n    \u003e =b:set(2, \"pple\")\n    apple\n    \u003e =b:append \"pie\"\n    applepie\n    \u003e =b:insert(-3, '-')\n    apple-pie\n    \u003e =b:insert(1, '('):append ')'\n    (apple-pie)\n    \u003e =b:assign \"abc\"\n    abc\n    \u003e =b:tohex \" \"\n    61 62 63\n    \u003e ='{ 0x'..b:tohex \", 0x\"..' }'\n    { 0x61, 0x62, 0x63 }\n    \u003e =b:alloc(5, \"a\")\n    aaaaa   5\n    \u003e =b:len()\n    5\n    \u003e =b:len(3)\n    3\n    \u003e =b\n    aaa\n    \u003e =b:free(), #b\n    nil     0\n    \u003e =b:assign \"abc\" :eq \"def\"\n    false\n    \u003e =b :cmp \"def\"\n    -1\n    \u003e =b:move(3, 2)\n    abbc\n    \u003e =b:clear()\n\n    \u003e =b:quote()\n    \"\\000\\000\\000\\000\"\n    \u003e\n\n.. _string: http://www.lua.org/manual/5.1/manual.html#5.4\n\n\nif you define ``LB_SUBBUFFER`` flags when compile lbuffer, the\nsubbuffer feature would enable. then you can use ``buffer.sub`` to get\na reference to original buffer, if you modify the reference buffer,\nthe original buffer will be modified as well. you can only have\n``buffer._SUBS_MAX`` subbuffer at the moment, this value can be\nmodified by define a macro named ``LB_SUBS_MAX``, the default value is\n4.\n\nthis is a example using subbuffer feature: ::\n\n    $ lua -lbuffer\n    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio\n    \u003e b = buffer \"apply\"\n    \u003e sb = b:sub(5,5)\n    \u003e =sb:assign \"epie\"\n    epie\n    \u003e sb2 = sb:sub(2,1)\n    \u003e =sb2:assign \"-\"\n    -\n    \u003e =b\n    apple-pie\n    \u003e\n\nand, beside all, buffer module has a pair of full featured pack/unpack\nfunctions. it can be used extract struct from binary text to lua, this\na example to read ``*.mo`` file created from ``*.po`` file, using\nmsgfmt: ::\n\n    -- read *.mo file\n    function read_mofile(b)\n        local info = b:unpack [[ {\n            magic = i,\n            revision = i,\n            nstrings = i,\n            orig_tab_offset = i,\n            trans_tab_offset = i,\n            hash_tab_size = i,\n            hash_tab_offset = i,\n        } ]]\n\n        local trans = {}\n        for i = 0, info.nstrings-1 do\n            local o_len, o_offset = b:unpack(info.orig_tab_offset+8*i+1, \"\u003cii\")\n            local t_len, t_offset = b:unpack(info.trans_tab_offset+8*i+1, \"\u003cii\")\n            local os = b:unpack(o_offset+1, \"s\")\n            local ts = b:unpack(t_offset+1, \"s\")\n            trans[os] = ts\n        end\n        return info, trans\n    end\n\nfor details, see reference of lbuffer below.\n\n\nreference\n=========\n\n.. _reference:\n\ncompatible functions\n--------------------\n\nthere are serveral functions that are redirected from standard string\nmodule:\n\n    * ``dump``\n    * ``find``\n    * ``format``\n    * ``gmatch``\n    * ``gsub``\n    * ``match``\n\nthey are just simply convert its buffer arguments to string, and call\nfunctions in string module, and set return string values (if any) to\nthe first buffer argument. so notice that they may allocate lots of\nmemory. It would better not use these functions.\n\nthe usage is just the same like the same functions in string module.\n\nthere are also some functions are same as string module, but they are\nrewriten in lbuffer for better performance, since they needn't copy\ndata from buffer and normal lua string:\n\n    * ``byte``\n    * ``char``\n    * ``len``\n    * ``lower``\n    * ``reverse``\n    * ``upper``\n\nnote that the ``len`` function has extended by lbuffer:\n\n- ``buffer.len([newlen])``\n\n    if ``newlen`` is given, the buffer will be expand/truncate to\n    newlen bytes, Extra bytes are set to ascii code 0.\n\nmodifie functions\n-----------------\n\nthere are serveral functions that used to modify buffer:\n    * ``new``\n    * ``append``\n    * ``assign``\n    * ``insert``\n    * ``set``\n\nnew is the constructor of buffer, and others are in lbuffer module, or\ncan indexed from a buffer object.\n\nthey accepts almost the same arguments, there are two forms, first is ::\n\n    modify([pos, ][len, ][b, [i[, j]]])\n\nonly ``append``, ``insert`` and ``set`` could have optional ``pos``\nargument, it means the position in buffer that modify makes. the\n``pos``, ``i`` and ``j`` arguments in all lbuffer argument are 1\nbased, just like array index in Lua. they can be negative, that means\nthe index is begin at the end of buffer.\n\nif ``len`` is given, exactly ``len`` bytes in buffer are\ninsert/modified, if ``b`` is bigger than ``len``, they will be\ntruncated.  and if ``b`` is smaller than ``len``, b will repeated\nuntil it reached to ``len``. if ``b`` is omitted, the ascii code 0\nwill be used.\n\ne.g. ::\n\n    \u003e =buffer(10, \"a\")\n    aaaaaaaaaa\n    \u003e =buffer(3, \"apple\")\n    app\n    \u003e =buffer(10, \"apple\")\n    appleapple\n    \u003e =buffer(10):quote()\n    \"\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\"\n    \u003e\n\n``b`` can be buffer or string. ``i`` and ``j`` locale the useful piece\nin ``b`` ::\n\n    \u003e =buffer(\"apple\", 2, -2)\n    ppl\n\nthe second form is ::\n\n    modify([pos, ]ud, len)\n\nit accept a userdata and a length, and reads ``len`` bytes from the\naddress of ``ud``, **be careful** with this form, it may very\ndangerous!!\n\nbinary pack functions\n---------------------\n\n* pack\n* unpack\n* getint\n* getuint\n* setint\n* setuint\n\nsubbuffer functions\n-------------------\n\n* sub\n* subcount\n* offset\n\nmisc functions\n--------------\n\n* alloc\n* clear\n* cmp\n* copy\n* eq\n* free\n* ipairs\n* isbuffer\n* move\n* quote\n* remove\n* swap\n* tohex\n* topointer\n* tostring\n\nC module developer note\n=======================\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarwing%2Flbuffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstarwing%2Flbuffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstarwing%2Flbuffer/lists"}