<commit_before>/*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.lib;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.luaj.vm.LFunction;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LuaState;


public class TableLib extends LFunction {

	public static final String[] NAMES = {
		"table",
		"concat",
		"getn",
		"insert",
		"maxn",
		"remove",
		"sort",
	};
	
	private static final int INSTALL = 0;
	private static final int CONCAT  = 1;
	private static final int GETN    = 2;
	private static final int INSERT  = 3;
	private static final int MAXN    = 4;
	private static final int REMOVE  = 5;
	private static final int SORT    = 6;
	
	public static void install( LTable globals ) {
		LTable table = new LTable();
		for ( int i=1; i<NAMES.length; i++ )
			table.put( NAMES[i], new TableLib(i) );
		globals.put( "table", table );
	}

	private final int id;

	/** Default constructor for loading the library dynamically */
	private TableLib() {
		id = 0;
	}

	/** Private constructor to construct a table function instance */
	private TableLib( int id ) {
		this.id = id;
	}

	public String toString() {
		return NAMES[id]+"()";
	}
		
	public boolean luaStackCall( LuaState vm ) {
		switch ( id ) {

		/* Load the table library dynamically 
		 */
		case INSTALL:
			install(vm._G);
			break;
			
			
		/* table.concat (table [, sep [, i [, j]]])
		 * 
		 * Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. 
		 * The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. 
		 * If i is greater than j, returns the empty string.
		 */
		case CONCAT: { 
			int n = vm.gettop();
			LTable table = vm.totable(2);
			LString sep = (n>=3? vm.tolstring(3): null);
			int i = vm.tointeger(4);
			int j = vm.tointeger(5);
			int len = table.luaLength();
			if ( i == 0 ) 
				i = 1;
			if ( j == 0 ) 
				j = len;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			try {
				for ( int k=i; k<=j; k++ ) {
					LValue v = table.get(k);
						v.luaAsString().write(baos);
						if ( k<j && sep!=null )
							sep.write( baos );
				}
				vm.resettop();
				vm.pushlstring( baos.toByteArray() );
			} catch (IOException e) {
				vm.error(e.getMessage());
			}
			break;
		}
		
		/* table.getn (table)
		 * 
		 * Get length of table t.
		 */ 
		case INSERT: { 
			int n = vm.gettop();
			LTable table = vm.totable(2);
			int pos = (n>=4? vm.tointeger(3): 0);
			LValue value = vm.topointer(-1);
			table.luaInsertPos( pos, value );
			break;
		}
		
		/* table.insert (table, [pos,] value)
		 * 
		 * Inserts element value at position pos in table, shifting up other elements to open space, if necessary. 
		 * The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call 
		 * table.insert(t,x) inserts x at the end of table t.
		 */ 
		case GETN: { 
			LTable table = vm.totable(2);
			vm.resettop();
			vm.pushinteger(table.luaLength());
			break;
		}

		/* table.maxn (table)
		 * 
		 * Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical 
		 * indices. (To do its job this function does a linear traversal of the whole table.)
		 */ 
		case MAXN: { 
			LTable table = vm.totable(2);
			vm.resettop();
			vm.pushinteger( table.luaMaxN() );
			break;
		}
			
		/* table.remove (table [, pos])
		 * 
		 * Removes from table the element at position pos, shifting down other elements to close the space, if necessary. 
		 * Returns the value of the removed element. The default value for pos is n, where n is the length of the table, 
		 * so that a call table.remove(t) removes the last element of table t.
		 */ 
		case REMOVE: { <commit_msg>Correct javadoc<commit_after>/*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.lib;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.luaj.vm.LFunction;
import org.luaj.vm.LString;
import org.luaj.vm.LTable;
import org.luaj.vm.LValue;
import org.luaj.vm.LuaState;


public class TableLib extends LFunction {

	public static final String[] NAMES = {
		"table",
		"concat",
		"getn",
		"insert",
		"maxn",
		"remove",
		"sort",
	};
	
	private static final int INSTALL = 0;
	private static final int CONCAT  = 1;
	private static final int GETN    = 2;
	private static final int INSERT  = 3;
	private static final int MAXN    = 4;
	private static final int REMOVE  = 5;
	private static final int SORT    = 6;
	
	public static void install( LTable globals ) {
		LTable table = new LTable();
		for ( int i=1; i<NAMES.length; i++ )
			table.put( NAMES[i], new TableLib(i) );
		globals.put( "table", table );
	}

	private final int id;

	/** Default constructor for loading the library dynamically */
	private TableLib() {
		id = 0;
	}

	/** Private constructor to construct a table function instance */
	private TableLib( int id ) {
		this.id = id;
	}

	public String toString() {
		return NAMES[id]+"()";
	}
		
	public boolean luaStackCall( LuaState vm ) {
		switch ( id ) {

		/* Load the table library dynamically 
		 */
		case INSTALL:
			install(vm._G);
			break;
			
			
		/* table.concat (table [, sep [, i [, j]]])
		 * 
		 * Given an array where all elements are strings or numbers, returns table[i]..sep..table[i+1] ··· sep..table[j]. 
		 * The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. 
		 * If i is greater than j, returns the empty string.
		 */
		case CONCAT: { 
			int n = vm.gettop();
			LTable table = vm.totable(2);
			LString sep = (n>=3? vm.tolstring(3): null);
			int i = vm.tointeger(4);
			int j = vm.tointeger(5);
			int len = table.luaLength();
			if ( i == 0 ) 
				i = 1;
			if ( j == 0 ) 
				j = len;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			try {
				for ( int k=i; k<=j; k++ ) {
					LValue v = table.get(k);
						v.luaAsString().write(baos);
						if ( k<j && sep!=null )
							sep.write( baos );
				}
				vm.resettop();
				vm.pushlstring( baos.toByteArray() );
			} catch (IOException e) {
				vm.error(e.getMessage());
			}
			break;
		}
		
		/* table.getn (table)
		 * 
		 * Get length of table t.
		 */ 
		case GETN: { 
			LTable table = vm.totable(2);
			vm.resettop();
			vm.pushinteger(table.luaLength());
			break;
		}
		
		/* table.insert (table, [pos,] value)
		 * 
		 * Inserts element value at position pos in table, shifting up other elements to open space, if necessary. 
		 * The default value for pos is n+1, where n is the length of the table (see §2.5.5), so that a call 
		 * table.insert(t,x) inserts x at the end of table t.
		 */ 
		case INSERT: { 
			int n = vm.gettop();
			LTable table = vm.totable(2);
			int pos = (n>=4? vm.tointeger(3): 0);
			LValue value = vm.topointer(-1);
			table.luaInsertPos( pos, value );
			break;
		}

		/* table.maxn (table)
		 * 
		 * Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical 
		 * indices. (To do its job this function does a linear traversal of the whole table.)
		 */ 
		case MAXN: { 
			LTable table = vm.totable(2);
			vm.resettop();
			vm.pushinteger( table.luaMaxN() );
			break;
		}
			
		/* table.remove (table [, pos])
		 * 
		 * Removes from table the element at position pos, shifting down other elements to close the space, if necessary. 
		 * Returns the value of the removed element. The default value for pos is n, where n is the length of the table, 
		 * so that a call table.remove(t) removes the last element of table t.
		 */ 
		case REMOVE: { 
{'fraction_non_alphanumeric': 0.13557654466745375, 'fraction_numerical': 0.006690279417552145, 'mean_word_length': 3.1489795918367345, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1348230', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3071, 'n_words': 1239}