lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Nov 29, 2011 at 1:21 PM, Petite Abeille
<petite.abeille@gmail.com> wrote:
> The extensive Penlight treatise mentions  "File-style I/O on Strings". Which is nice.
> Would anyone have the reverse? String-style methods on Files?
> E.g.:
> local aFile = io.open( 'test.txt', 'rb' )
> if aFile:find( 'foo', 1, true ) do
>  ...
> end
> In other words, string methods [2] working in terms of a file object? :)

I once ported the Lua 5.1 string library to pure Lua [1].  Here,
strings are represented as Lua tables of characters, and those tables
can be proxy tables to other things like files (see "filearray.lua"
example).

The caveat is that this is a straight line-by-line port of the C code
and therefore probably not the most efficient it could be.  So, I'm
not sure if this would be of any practical use to anyone.  In most
cases you can probably instead just read the entire file into a string
in memory and use the regular string library or read chunk-by-chunk
and do string matching on each individual chunk (appropriately taking
boundaries into account).  Another approach may be to use some
memory-mapped file support on the OS.

[1] http://lua-users.org/wiki/StringLibraryInLua