git 3 weeks ago
parent
commit
5a5e4dbf87
2 changed files with 21 additions and 11 deletions
  1. 18 5
      src/main/kotlin/cn/qqck/kotlin/tools/String.kt
  2. 3 6
      src/test/kotlin/Main.kt

+ 18 - 5
src/main/kotlin/cn/qqck/kotlin/tools/String.kt

@@ -187,13 +187,26 @@ fun String.hide(hs: Char = '*', pre_len: Int = 2, suf_len: Int = 2, hs_max: Int?
     val j_slen = this.length
     if (j_slen <= pre_len + suf_len) return this
     val j_midlen = j_slen - pre_len - suf_len
-    val j_shide = CharArray(j_slen)
-    for (j_i in 0 until pre_len) j_shide[j_i] = this[j_i]
+    val j_shide = CharArray(if (hs_max == null || j_midlen < hs_max) j_slen else (j_slen - j_midlen + hs_max))
+    var j_si = 0
+    while (j_si < pre_len) {
+        j_shide[j_si] = this[j_si]
+        j_si++
+    }
     if (hs_max == null || j_midlen < hs_max) {
-        for (j_i in pre_len until pre_len + j_midlen) j_shide[j_i] = hs
+        for (j_i in 0..<j_midlen) {
+            j_shide[j_si] = hs
+            j_si++
+        }
     } else {
-        for (j_i in pre_len until pre_len + hs_max) j_shide[j_i] = hs
+        for (j_i in 0..<hs_max) {
+            j_shide[j_si] = hs
+            j_si++
+        }
+    }
+    for (j_i in j_slen - suf_len until j_slen) {
+        j_shide[j_si] = this[j_i]
+        j_si++
     }
-    for (j_i in j_slen - suf_len until j_slen) j_shide[j_i] = this[j_i]
     return String(j_shide)
 }

+ 3 - 6
src/test/kotlin/Main.kt

@@ -1,17 +1,14 @@
-import cn.qqck.kotlin.tools.Rand
-import cn.qqck.kotlin.tools.fget
-import cn.qqck.kotlin.tools.fset
-import cn.qqck.kotlin.tools.string
+import cn.qqck.kotlin.tools.*
 
 fun main() {
     // println("Date.day(1)".toIntOrNull())
     // println("192.168.1.1".isIpv4())
-    // println("2409:8c1e:75b0:1120::16".isIpv6())
+    println("2409:8c1e:75b0:1120::16".hide(pre_len = 5, suf_len = 7, hs_max = 3))
     // println("fe80::1ff:fe23:4567:890a".isIpv6())
     // println(Android.random())
     // println(register_queue.isNet())
     // register_queue.get()
-    ggcz_用户()
+    // ggcz_用户()
 }
 
 fun ggcz_用户() {