Aardio-custom library to implement extended system functions.

Realize function:

Extended kernel library functions

  • tonumberEx(__) = Convert to numeric value (enhanced version)\nIf the parameter is a decimal, return the value rounded to 9 digits after the decimal point;\ncan replace tonumber()
  • toHex(__) = Take hexadecimal text. \nSupport numeric number, text string, buffer, pointer. \nIf it is string, buffer, ponter, you can take parameters:\n @2: Specify the starting position (can be a negative number)\n @3: Specify the length of the data to be converted.
  • hexToNumber(__) = Hexadecimal text to numeric value
  • hexToString(__) = Hexadecimal text to text
  • hexToBuffer(__) = Hexadecimal text to buffer
  •  
  • string.utf8ToAnsi(__) = utf8 to ansi. The code is a constant starting with _CHARSET_.
  • string.utf8ToGbk(__) = utf8 to GBK. The code is a constant starting with _CHARSET_.
  • string.utf8ToUnicode(__) = utf8 to unicode(UTF16). The code is a constant starting with _CHARSET_.
  • string.ansiToUtf8(__) = ansi to utf8. The code is a constant starting with _CHARSET_.
  • string.ansiToUnicode(__) = ansi to unicode(UTF16). The code is a constant starting with _CHARSET_.
  • string.ansiToGbk(__) = ansi to gbk. The code is a constant starting with _CHARSET_.
  • string.unicodeToUtf8(__) = unicode(UTF16) to utf8. The code is a constant starting with _CHARSET_.
  • string.unicodeToAnsi(__) = unicode(UTF16) to ansi. The code is a constant starting with _CHARSET_.
  • string.unicodeToGbk(__) = unicode(UTF16) to gbk. The code is a constant starting with _CHARSET_.
  • table.sortEx(__,1,false,0) = Table sorting. \nModify the original table directly, and return true if the modification is successful. \nParameter 1: The table to be sorted. If each item is a table (sub-table), you need to specify the reference field for sorting through parameter 2;\nParameter 2: The reference field (array index or dictionary key name) of the sub-table to be sorted;\n if the sub-item If it is an array, the reference field defaults to 1\nParameter 3: Whether to reverse the order. \nParameter 4: Convert the data before sorting, 1 is converted to time, 2 is converted to numerical value, 3 is converted to text, and the others are not converted.
     
  • _CHARSET_UTF8 = @65001/*_CHARSET_UTF8*/
  • _CHARSET_UTF16 = @1200/*_CHARSET_UTF16*/
  • _CHARSET_UNICODE = @1200/*_CHARSET_UNICODE*/
  • _CHARSET_BIG5 = @950/*_CHARSET_BIG5*/
  • _CHARSET_GBK = @936/*_CHARSET_GBK*/
  • _CHARSET_ANSI = @0x0/*_CHARSET_ANSI*/
  • _CHARSET_GB18030 = @54936/*_CHARSET_GB18030*/
  • _CHARSET_GB2312 = @936/*_CHARSET_GB2312*/
     

Library file location:

~ \ lib \ godking.aardio 

Library code: 

namespace godking

..tonumberEx = function(num,...){
    if (type(num)=="number"){
        if (..math.isInteger(num)) 
        return num;
   		return ..math.round(num,9);
   	}   return ..tonumber(num,...);
}

..toHex = function(v,s,l){
    select(type(v)) {
    	case "number" {
    		return ..string.format("%X",v);	
    	}
    	case "string","buffer" {
    		if (s?l) return ..toHex(..raw.toPointer(v),s,l); 
    		else return ..string.hex(v,"");	
    	}
    	case "pointer" {
    		s:=1
    		l:=1
   			var b = ..raw.buffer(l)
   			..raw.copy(b,..topointer((..tonumber(v)+s-1)),l)
   			return ..string.hex(b,"");
    	}
	}
}


..string.utf8ToAnsi = function(str){
	return ..string.fromto(str,65001/*_CHARSET_UTF8*/,0x0/*_CHARSET_ANSI*/);
}

..string.utf8ToGbk = function(str){
	return ..string.fromto(str,65001/*_CHARSET_UTF8*/,936/*_CHARSET_GBK*/);
}

..string.utf8ToUnicode = function(str){
	return ..string.fromto(str,65001/*_CHARSET_UTF8*/,1200/*_CHARSET_UNICODE*/);
}

..string.ansiToUtf8 = function(str){
	return ..string.fromto(str,0x0/*_CHARSET_ANSI*/,65001/*_CHARSET_UTF8*/);	
}

..string.ansiToUnicode = function(str){
	return ..string.fromto(str,0x0/*_CHARSET_ANSI*/,1200/*_CHARSET_UNICODE*/);
}

..string.ansiToGbk = function(str){
	return ..string.fromto(str,0x0/*_CHARSET_ANSI*/,936/*_CHARSET_GBK*/);	
}

..string.unicodeToUtf8 = function(str){
	return ..string.fromto(str,1200/*_CHARSET_UNICODE*/,65001/*_CHARSET_UTF8*/);	
}

..string.unicodeToAnsi = function(str){
	return ..string.fromto(str,1200/*_CHARSET_UNICODE*/,0x0/*_CHARSET_ANSI*/);	
}

..string.unicodeToGbk = function(str){
	return ..string.fromto(str,1200/*_CHARSET_UNICODE*/,936/*_CHARSET_GBK*/);	
}

..hexToNumber = function(hex){
	return ..tonumber(hex,16); 	
}

..hexToString = function(hex){
	return ..string.unhex(hex,""); 
}

..hexToBuffer = function(hex){
	return ..raw.buffer(..string.unhex(hex,"")); 
}

..table.sortEx = function(array,key,desc=false,convert=0){
    if (!array) return ; 
    if (type(array)!="table") return ;
    if (..table.count(array)=0) return ;  
    //对数组进行排序
	if (type(array[1])!="table"){
		//如果没有数组成员则返回
		if(!#array) return ; 
		if (convert=1/*转换为时间*/){
			if(desc){
				..table.sort(array,function(b){
					return ..time(owner) > ..time(b)
				})	
			}else{
				..table.sort(array,function(b){
					return ..time(owner) < ..time(b)
				})
			}
		}elseif (convert=2/*转换为数值*/){
			if(desc){
				..table.sort(array,function(b){
					return ..tonumber(owner) > ..tonumber(b)
				})	
			}else{
				..table.sort(array,function(b){
					return ..tonumber(owner) < ..tonumber(b)
				})
			}
		}elseif (convert=3/*转换为文本*/){
			if(desc){
				..table.sort(array,function(b){
					return ..tostring(owner) > ..tostring(b)
				})	
			}else{
				..table.sort(array,function(b){
					return ..tostring(owner) < ..tostring(b)
				})
			}
		}else/*不进行转换处理*/{
			if(desc){
				..table.sort(array,function(b){
					return owner > b
				})	
			}else{
				..table.sort(array,function(b){
					return owner < b
				})
			}
		}
		return true; 
	}
	//处理子表合法性
	if (#array[1]){
		key:=1
		if (type(key)!="number") return ;
	}else{
		key:=..table.keys(array[1])[1]
		if (!key or type(key)!="string") return ;
	}
	//进行子表排序
	if(convert=1/*转换为时间*/){
		if(desc){
			..table.sort(array,function(b){
				return ..time(owner[key]) > ..time(b[key])
			})	
		}
		else {
			..table.sort(array,function(b){
				return ..time(owner[key]) < ..time(b[key])
			})
		}
	}elseif(convert=2/*转换为数值*/){
		if(desc){
			..table.sort(array,function(b){
				return ..tonumber(owner[key]) > ..tonumber(b[key])
			})	
		}
		else {
			..table.sort(array,function(b){
				return ..tonumber(owner[key]) < ..tonumber(b[key])
			})
		}
	}elseif(convert=3/*转换为文本*/){
		if(desc){
			..table.sort(array,function(b){
				return ..tostring(owner[key]) > ..tostring(b[key])
			})	
		}
		else {
			..table.sort(array,function(b){
				return ..tostring(owner[key]) < ..tostring(b[key])
			})
		}
	}else/*不进行转换处理*/{
		if(desc){
			..table.sort(array,function(b){
				return owner[key] > b[key]
			})	
		}
		else {
			..table.sort(array,function(b){
				return owner[key] < b[key]
			})
		}
	}
	return true; 	
}
 

/**intellisense()
tonumberEx(__) = 转换为数值(加强版)\n参数如果为小数,返回四舍五入到小数点后9位的数值;\n可代替tonumber()
toHex(__) = 取十六进制文本。\n支持数值number、文本string、缓冲区buffer、指针pointer。\n如果是string、buffer、ponter,可以带参数:\n    @2:指定开始位置(可以为负数)\n    @3:指定要转换的数据长度。
hexToNumber(__) = 十六进制文本转数值
hexToString(__) = 十六进制文本转文本
hexToBuffer(__) = 十六进制文本转缓冲区
end intellisense**/

/**intellisense()
string.utf8ToAnsi(__) = utf8转ansi。编码为_CHARSET_开头的常量。
string.utf8ToGbk(__) = utf8转GBK。编码为_CHARSET_开头的常量。
string.utf8ToUnicode(__) = utf8转unicode(UTF16)。编码为_CHARSET_开头的常量。
string.ansiToUtf8(__) = ansi转utf8。编码为_CHARSET_开头的常量。
string.ansiToUnicode(__) = ansi转unicode(UTF16)。编码为_CHARSET_开头的常量。
string.ansiToGbk(__) = ansi转gbk。编码为_CHARSET_开头的常量。
string.unicodeToUtf8(__) = unicode(UTF16)转utf8。编码为_CHARSET_开头的常量。
string.unicodeToAnsi(__) = unicode(UTF16)转ansi。编码为_CHARSET_开头的常量。
string.unicodeToGbk(__) = unicode(UTF16)转gbk。编码为_CHARSET_开头的常量。

table.sortEx(__,1,false,0) = 表排序。\n直接修改原表,修改成功返回true。\n参数1:要排序的表。如果每一项都是表(子表),则需要通过参数2指定排序的参考字段;\n参数2:要排序的子表的参考字段(数组索引或字典键名);\n       如果子项为数组,则参考字段默认为1\n参数3:是否逆序。\n参数4:将数据进行转换后再排序,1转换为时间,2转换为数值,3转换为文本,其它不转换。
end intellisense**/

/**intellisense()
_CHARSET_UTF8 = @65001/*_CHARSET_UTF8*/
_CHARSET_UTF16 = @1200/*_CHARSET_UTF16*/
_CHARSET_UNICODE = @1200/*_CHARSET_UNICODE*/
_CHARSET_BIG5 = @950/*_CHARSET_BIG5*/
_CHARSET_GBK = @936/*_CHARSET_GBK*/
_CHARSET_ANSI = @0x0/*_CHARSET_ANSI*/
_CHARSET_GB18030 = @54936/*_CHARSET_GB18030*/
_CHARSET_GB2312 = @936/*_CHARSET_GB2312*/
end intellisense**/

Call example:

import console; 
import godking;

var d1 = 0.575*100
var d2 = tonumberEx(0.575*100)
console.log("未处理的不精确值 ->",string.format("%.16f",d1 ))
console.log("处理过的精确值 ->",string.format("%.16f",d2 ))

var num = 12345678
var txt="12345678"
var buffer = raw.buffer(txt);
var pointer = raw.toPointer(buffer)
console.log(toHex(num))
console.log(toHex(txt))
console.log(toHex(buffer))
console.log(toHex(pointer,-8,20))

var utf8 = "这里是utf8字符串"
var ansi = string.utf8ToAnsi(utf8)
var unicode = string.ansiToUnicode(ansi)
console.log("utf8->",toHex(utf8))
console.log("ansi->",toHex(ansi))
console.log("unicode->",toHex(unicode))

console.pause(true);

Results of the:

Guess you like

Origin blog.csdn.net/sdlgq/article/details/112394376