src/t_csv_l.c
static int
t_csv_split( lua_State *L )
{
	size_t       lns,lnd; // length input, length delim
	const char  *str = luaL_checklstring( L, lua_upvalueindex( 1 ), &lns );
	const char  *dlm = luaL_checklstring( L, lua_upvalueindex( 2 ), &lnd );
	const int    dst = lua_toboolean( L, lua_upvalueindex( 3 ) );     // don't strip delimiter?
	size_t       its = luaL_checkinteger( L, lua_upvalueindex( 4 ) ); // iter input
	size_t       cnt = luaL_checkinteger( L, lua_upvalueindex( 5 ) ); // token counter
	size_t       itd = 0, tkn = its;                                  // iter delim, token start
	if (its > lns)
		lua_pushnil( L );
	else
	{
		for (; its < lns; its++)
		{
			//printf("   ITS: %zu <%c><%c> ", its, str[its], dlm[itd]);
			itd = (str[its] == dlm[itd])  // string matches delimiter
				? itd+1                    // advance delimiter
				: (str[its] == *dlm)       // first character in delimiter matches
					? 1                     // advance delimiter to one
					: 0;                    // reset delimiter
			//printf(" -- %s\n", lnd == itd ? "Yea" : "Nah");
			if (itd == lnd)               // delimeter completely matched
				break;
		}
		//printf("      TKN: %s  %zu %ld \n", &(str[tkn]), its, its-tkn  - ((its<lns) ? ((dst==1) ? -1 : lnd-1) : 0) );
		lua_pushlstring( L, &(str[tkn]), its-tkn -
			((its<lns)            // Not end of input text?
				? ((dst==1)        // strip delimiter?
					? -1            // Include delimiter
					: lnd-1)        // Exclude delimiter
				: 0)               // last field
		);
		lua_pushinteger( L, its+1 );
		lua_replace( L, lua_upvalueindex( 4 ) );    // update string iterator position
		lua_pushinteger( L, cnt+1 );
		lua_replace( L, lua_upvalueindex( 5 ) );    // update field count
		lua_pushinteger( L, cnt+1 );
	}
	return (its>lns) ? 1 : 2;
}
/**--------------------------------------------------------------------------
 * Closure to split a string by a delimiter
 * \param   L       Lua state.
 * \uparam  string  Original text string.
 * \uparam  string  Delimiter string.
 * \uparam  int     Index inside uparam text string.
 * \lreturn string  Last token that was split of from text string.
 * \return  int     # of values pushed onto the stack.
 * --------------------------------------------------------------------------*/
Closure to split a string by a delimiter
\param   L       Lua state.
\uparam  string  Original text string.
\uparam  string  Delimiter string.
\uparam  int     Index inside uparam text string.
\lreturn string  Last token that was split of from text string.
\return  int     # of values pushed onto the stack.
Closure to split a string by a delimiter \ param L Lua state . \ uparam string Original text string . \ uparam string Delimiter string . \ uparam int Index inside uparam text string . \ lreturn string Last token that was split of from text string . \ return int # of values pushed onto the stack .
static int
t_csv_split( lua_State *L )
{
	size_t       lns,lnd; 
	const char  *str = luaL_checklstring( L, lua_upvalueindex( 1 ), &lns );
	const char  *dlm = luaL_checklstring( L, lua_upvalueindex( 2 ), &lnd );
	const int    dst = lua_toboolean( L, lua_upvalueindex( 3 ) );     
	size_t       its = luaL_checkinteger( L, lua_upvalueindex( 4 ) ); 
	size_t       cnt = luaL_checkinteger( L, lua_upvalueindex( 5 ) ); 
	size_t       itd = 0, tkn = its;                                  
	if (its > lns)
		lua_pushnil( L );
	else
	{
		for (; its < lns; its++)
		{
			itd = (str[its] == dlm[itd])  
				? itd+1                    
				: (str[its] == *dlm)       
					? 1                      to one
					: 0;                    
			if (itd == lnd)               
				break;
		}
		lua_pushlstring( L, &(str[tkn]), its-tkn -
			((its<lns)            
				? ((dst==1)        
					? -1            
					: lnd-1)        
				: 0)               
		);
		lua_pushinteger( L, its+1 );
		lua_replace( L, lua_upvalueindex( 4 ) );    
		lua_pushinteger( L, cnt+1 );
		lua_replace( L, lua_upvalueindex( 5 ) );    
		lua_pushinteger( L, cnt+1 );
	}
	return (its>lns) ? 1 : 2;
}
static int t_csv_split ( lua_State * L ) { size_t lns , lnd ; const char * str = luaL_checklstring ( L , lua_upvalueindex ( 1 ) , & lns ) ; const char * dlm = luaL_checklstring ( L , lua_upvalueindex ( 2 ) , & lnd ) ; const int dst = lua_toboolean ( L , lua_upvalueindex ( 3 ) ) ; size_t its = luaL_checkinteger ( L , lua_upvalueindex ( 4 ) ) ; size_t cnt = luaL_checkinteger ( L , lua_upvalueindex ( 5 ) ) ; size_t itd = 0 , tkn = its ; if ( its > lns ) lua_pushnil ( L ) ; else { for ( ; its < lns ; its ++ ) { itd = ( str [ its ] == dlm [ itd ] ) ? itd + 1 : ( str [ its ] == * dlm ) ? 1 : 0 ; if ( itd == lnd ) break ; } lua_pushlstring ( L , & ( str [ tkn ] ) , its - tkn - ( ( its < lns ) ? ( ( dst == 1 ) ? -1 : lnd - 1 ) : 0 ) ) ; lua_pushinteger ( L , its + 1 ) ; lua_replace ( L , lua_upvalueindex ( 4 ) ) ; lua_pushinteger ( L , cnt + 1 ) ; lua_replace ( L , lua_upvalueindex ( 5 ) ) ; lua_pushinteger ( L , cnt + 1 ) ; } return ( its > lns ) ? 1 : 2 ; }
Closure to split a string by a delimiter
 \param   L       Lua state.
Closure to split a string by a delimiter \ param L Lua state .
// length input, length delim // don't strip delimiter? // iter input // token counter // iter delim, token start //printf("   ITS: %zu <%c><%c> ", its, str[its], dlm[itd]); // string matches delimiter // advance delimiter // first character in delimiter matches // advance delimiter to one // reset delimiter //printf(" -- %s\n", lnd == itd ? "Yea" : "Nah"); // delimeter completely matched //printf("      TKN: %s  %zu %ld \n", &(str[tkn]), its, its-tkn  - ((its<lns) ? ((dst==1) ? -1 : lnd-1) : 0) ); // Not end of input text? // strip delimiter? // Include delimiter // Exclude delimiter // last field // update string iterator position // update field count