string_demo.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. --[[
  2. @summary 字符串操作功能演示脚本
  3. @version 1.0
  4. @date 2025.07.16
  5. @author 陈媛媛
  6. @usage
  7. 本demo通过多个示例演示string核心库的主要功能:
  8. 1、十六进制编码/解码:演示字符串与十六进制格式的互相转换
  9. 2、字符串分割处理:演示使用不同分隔符进行字符串分割
  10. 3、数值转换操作:演示字符串到二进制数据的转换
  11. 4、Base64编码解码:演示Base64编码和解码功能
  12. 5、Base32编码解码:演示Base32编码和解码功能
  13. 6、字符串前后缀判断:演示字符串前缀和后缀判断功能
  14. 7、字符串裁剪处理:演示去除字符串前后空白字符的功能
  15. ]]
  16. -- 初始化函数
  17. local function init()
  18. log.info("string_demo", "字符串操作演示脚本初始化")
  19. end
  20. -- ====================== 十六进制转换示例 ======================
  21. local function hex_examples()
  22. log.info("=== 十六进制转换示例 ===")
  23. -- 字符串转十六进制
  24. local original_str = "123abc"
  25. local hex_result, hex_length = string.toHex(original_str)
  26. log.info("toHex", "原始字符串:", original_str, "HEX结果:", hex_result, "长度:", hex_length)
  27. -- 带分隔符的十六进制转换
  28. local hex_with_space = string.toHex(original_str, " ")
  29. log.info("toHex", "带空格分隔:", hex_with_space)
  30. -- 十六进制转字符串
  31. local decoded_str = string.fromHex("313233616263")
  32. log.info("fromHex", "HEX字符串解码:", decoded_str)
  33. -- 二进制数据转换
  34. local binary_data = string.char(0x01, 0x02, 0x03, 0x04, 0x05)
  35. local binary_hex = string.toHex(binary_data)
  36. log.info("toHex", "二进制数据转HEX:", binary_hex)
  37. end
  38. -- ====================== 字符串分割示例 ======================
  39. local function split_examples()
  40. log.info("=== 字符串分割示例 ===")
  41. -- 基本分割
  42. local csv_str = "123,456,789"
  43. local parts1 = string.split(csv_str, ",")
  44. log.info("split", "CSV分割结果:", #parts1, json.encode(parts1))
  45. -- 使用小数点作为分隔符
  46. local ip_str = "192.168.1.1"
  47. local ip_parts = string.split(ip_str, "%.")
  48. log.info("split", "IP地址分割:", #ip_parts, json.encode(ip_parts))
  49. -- 多字符分隔符
  50. local multi_sep = "a||b||c||d"
  51. local multi_parts = string.split(multi_sep, "||")
  52. log.info("split", "多字符分隔符:", #multi_parts, json.encode(multi_parts))
  53. end
  54. -- ====================== 数值转换示例 ======================
  55. local function value_conversion_examples()
  56. log.info("=== 数值转换示例 ===")
  57. -- 字符串转数值编码
  58. local num_str = "123456"
  59. local binary_result, converted_chars = string.toValue(num_str)
  60. log.info("toValue", "原始字符串:", num_str, "转换字符数:", converted_chars)
  61. log.info("toValue", "二进制结果HEX:", string.toHex(binary_result))
  62. -- 包含字母的转换
  63. local mixed_str = "123abc"
  64. local mixed_result, mixed_count = string.toValue(mixed_str)
  65. log.info("toValue", "混合字符串转换:", mixed_str, "字符数:", mixed_count)
  66. log.info("toValue", "混合结果HEX:", string.toHex(mixed_result))
  67. end
  68. -- ====================== Base64编码示例 ======================
  69. local function base64_examples()
  70. log.info("=== Base64编码示例 ===")
  71. -- Base64编码
  72. local plain_text = "Hello LuaOS!"
  73. local encoded_b64 = string.toBase64(plain_text)
  74. log.info("toBase64", "原文:", plain_text, "编码后:", encoded_b64)
  75. -- Base64解码
  76. local decoded_b64 = string.fromBase64(encoded_b64)
  77. log.info("fromBase64", "解码结果:", decoded_b64)
  78. -- 中文编码
  79. local chinese_text = "你好世界"
  80. local chinese_b64 = string.toBase64(chinese_text)
  81. log.info("toBase64", "中文原文:", chinese_text, "编码后:", chinese_b64)
  82. end
  83. -- ====================== Base32编码解码示例 ======================
  84. local function base32_examples()
  85. log.info("=== Base32编码解码示例 ===")
  86. -- 检查Base32功能是否可用
  87. if string.toBase32 and string.fromBase32 then
  88. -- 基本编码
  89. local plain_text = "Hello World"
  90. local encoded_b32 = string.toBase32(plain_text)
  91. log.info("toBase32", "原文:", plain_text, "编码后:", encoded_b32)
  92. -- 解码验证
  93. local decoded_b32 = string.fromBase32(encoded_b32)
  94. log.info("fromBase32", "解码结果:", decoded_b32)
  95. -- 短字符串编码
  96. local short_text = "test"
  97. local short_b32 = string.toBase32(short_text)
  98. log.info("toBase32", "短字符串编码:", short_text, "=>", short_b32)
  99. log.info("fromBase32", "短字符串解码:", string.fromBase32(short_b32))
  100. -- 中文编码
  101. local chinese_text = "你好世界"
  102. local chinese_b32 = string.toBase32(chinese_text)
  103. log.info("toBase32", "中文原文:", chinese_text, "编码后:", chinese_b32)
  104. log.info("fromBase32", "中文解码:", string.fromBase32(chinese_b32))
  105. -- 二进制数据编码
  106. local binary_data = string.char(0x01, 0x02, 0x03, 0x04, 0x05)
  107. local binary_b32 = string.toBase32(binary_data)
  108. log.info("toBase32", "二进制数据HEX:", string.toHex(binary_data), "Base32:", binary_b32)
  109. -- 空字符串处理
  110. local empty_b32 = string.toBase32("")
  111. log.info("toBase32", "空字符串编码:", "''", "=>", empty_b32)
  112. log.info("fromBase32", "空字符串解码:", "'" .. string.fromBase32(empty_b32) .. "'")
  113. else
  114. log.warn("Base32", "当前固件不支持Base32编码解码功能")
  115. log.info("Base32", "请确保使用支持Base32的LuatOS固件版本")
  116. end
  117. end
  118. -- ====================== 字符串判断示例 ======================
  119. local function string_check_examples()
  120. log.info("=== 字符串判断示例 ===")
  121. local test_str = "hello world"
  122. -- 判断前缀
  123. local starts_with_hello = string.startsWith(test_str, "hello")
  124. local starts_with_world = string.startsWith(test_str, "world")
  125. log.info("startsWith", "字符串:", test_str)
  126. log.info("startsWith", "以'hello'开头:", starts_with_hello)
  127. log.info("startsWith", "以'world'开头:", starts_with_world)
  128. -- 判断后缀
  129. local ends_with_world = string.endsWith(test_str, "world")
  130. local ends_with_hello = string.endsWith(test_str, "hello")
  131. log.info("endsWith", "以'world'结尾:", ends_with_world)
  132. log.info("endsWith", "以'hello'结尾:", ends_with_hello)
  133. end
  134. -- ====================== 字符串裁剪示例 ======================
  135. local function trim_examples()
  136. log.info("=== 字符串裁剪示例 ===")
  137. -- 包含空白字符的字符串
  138. local dirty_str = " \r\n hello world \t\n "
  139. -- 默认裁剪(前后都裁剪)
  140. local trimmed = string.trim(dirty_str)
  141. log.info("trim", "原始字符串长度:", #dirty_str)
  142. log.info("trim", "裁剪后字符串:", "'" .. trimmed .. "'", "长度:", #trimmed)
  143. -- 用户输入清理示例
  144. local user_input = " admin "
  145. local clean_input = string.trim(user_input)
  146. log.info("trim", "用户输入清理:", "'" .. user_input .. "'", "=>", "'" .. clean_input .. "'")
  147. end
  148. -- ====================== 主演示函数 ======================
  149. local function run_all_demos()
  150. log.info("string_demo", "开始执行所有字符串操作演示")
  151. init()
  152. hex_examples()
  153. split_examples()
  154. value_conversion_examples()
  155. base64_examples()
  156. base32_examples()
  157. string_check_examples()
  158. trim_examples()
  159. log.info("string_demo", "所有演示执行完成")
  160. end
  161. -- 主任务函数
  162. local function main()
  163. log.info("string_demo", "开始运行字符串操作演示")
  164. run_all_demos()
  165. log.info("string_demo", "演示运行完毕")
  166. end
  167. -- 启动演示任务
  168. sys.taskInit(main)