Skip to content

Commit 851f314

Browse files
committed
Merge pull request #7 from zakattacktwitter/master
Adding extname to complement basename.
2 parents 328d5c1 + eec27d0 commit 851f314

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ This is similar to the well know shell command `"basename"`.
4141
Return the name of directory containing file `path`.
4242
This is similar to the well known shell command `"dirname"`.
4343

44+
<a name="paths.extname"/>
45+
### paths.extname(path) ###
46+
47+
Return the extension of the `path` or nil if none is found.
48+
4449
<a name="paths.concat"/>
4550
### paths.concat([path1,....,pathn]) ###
4651

paths.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,23 @@ lua_dirname(lua_State *L)
339339
}
340340

341341

342+
static int
343+
lua_extname(lua_State *L)
344+
{
345+
const char *fname = luaL_checkstring(L, 1);
346+
const char *p;
347+
348+
p = fname + strlen(fname) - 1;
349+
while (p >= fname) {
350+
if (*p == '.') {
351+
lua_pushstring(L, p + 1);
352+
return 1;
353+
}
354+
p--;
355+
}
356+
return 0;
357+
}
358+
342359

343360
/* ------------------------------------------------------ */
344361
/* cwd and concat */
@@ -1095,6 +1112,7 @@ static const struct luaL_Reg paths__ [] = {
10951112
{"dirp", lua_dirp},
10961113
{"basename", lua_basename},
10971114
{"dirname", lua_dirname},
1115+
{"extname", lua_extname},
10981116
{"cwd", lua_cwd},
10991117
{"concat", lua_concatfname},
11001118
{"execdir", lua_execdir},

0 commit comments

Comments
 (0)