找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2751|回复: 2

vb StringBuilder类

[复制链接]
发表于 2019-7-25 20:23:27 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 天马座 于 2022-6-23 11:30 编辑

此类主要解决vb字符串频繁连接速度慢的问题,支持UTF-8 UTF-16互转,其他功能陆续添加

  1. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
  2. Private s As String
  3. Private size As Long, capacity As Long
  4. Private Sub Class_Initialize()
  5.     size = 0
  6.     capacity = 8
  7.     s = String(capacity \ 2, ChrW(0))
  8. End Sub
  9. Public Function length() As Long
  10.     '返回字符串长度 宽字节
  11.     length = size \ 2
  12. End Function
  13. Public Function lengthB() As Long
  14.     '返回字符串长度 字节
  15.     lengthB = size
  16. End Function
  17. Public Sub clear()
  18.     '清除缓冲区
  19.     size = 0
  20.     capacity = 8
  21.     s = String(capacity \ 2, ChrW(0))
  22. End Sub
  23. Public Sub append(ByRef value As String)
  24.     '添加
  25.     Call copyB(value, size)
  26. End Sub
  27. Public Sub copy(ByRef value As String, ByVal index As Long)
  28.     '写入 index对应宽字节 put命名被占用 改为copy
  29.     Call copyB(value, index * 2)
  30. End Sub
  31. Public Sub copyB(ByRef value As String, ByVal index As Long)
  32.     '写入 index对应字节
  33.     If index < 0 Or index > size Or LenB(value) = 0 Then
  34.         Exit Sub
  35.     End If
  36.     Dim n As Long
  37.     n = LenB(value)
  38.     Call ensureCapacity(index + n)
  39.     Call CopyMemory(ByVal unsignedAdd(StrPtr(s), index), ByVal StrPtr(value), n)
  40.     If size < index + n Then
  41.          size = index + n
  42.     End If
  43. End Sub
  44. Public Sub insert(ByRef value As String, ByVal index As Long)
  45.     '插入 index对应宽字节
  46.     Call insertB(value, index * 2)
  47. End Sub
  48. Public Sub insertB(ByRef value As String, ByVal index As Long)
  49.     '插入 index对应字节
  50.     If index < 0 Or index > size Or LenB(value) = 0 Then
  51.         Exit Sub
  52.     End If
  53.     Dim n As Long
  54.     n = LenB(value)
  55.     Call ensureCapacity(size + n)
  56.     If index <> size Then
  57.            Call CopyMemory(ByVal unsignedAdd(StrPtr(s), index + n), ByVal unsignedAdd(StrPtr(s), index), size - index)
  58.     End If
  59.     Call CopyMemory(ByVal unsignedAdd(StrPtr(s), index), ByVal StrPtr(value), n)
  60.     size = size + n
  61. End Sub
  62. Public Sub remove(ByVal index As Long)
  63.     '删除 index对应宽字节
  64.     Call removeRange0(index * 2, 2)
  65. End Sub
  66. Public Sub removeB(ByVal index As Long)
  67.     '删除 index对应字节
  68.     Call removeRange0(index, 1)
  69. End Sub
  70. Public Sub removeRange(ByVal lo As Long, ByVal hi As Long)
  71.     '删除 [lo..hi]对应宽字节
  72.     Call removeRange0(lo * 2, (hi - lo + 1) * 2)
  73. End Sub
  74. Public Sub removeRangeB(ByVal lo As Long, ByVal hi As Long)
  75.      '删除 [lo..hi]对应字节
  76.      Call removeRange0(lo, hi - lo + 1)
  77. End Sub
  78. Private Sub removeRange0(ByVal lo As Long, ByVal n As Long)
  79.     If n <= 0 Or lo < 0 Or lo + n > size Then
  80.         Exit Sub
  81.     End If
  82.     If size - (lo + n) > 0 Then
  83.         Call CopyMemory(ByVal unsignedAdd(StrPtr(s), lo), ByVal unsignedAdd(StrPtr(s), lo + n), size - (lo + n))
  84.     End If
  85.     size = size - n
  86. End Sub
  87. Public Sub replaceRange(ByVal lo As Long, ByVal hi As Long, ByRef oldValue As String, ByRef nweValue As String)
  88.     '替换[lo..hi]对应宽字节
  89.     Call replaceRangeB(lo * 2, hi * 2, oldValue, nweValue)
  90. End Sub
  91. Public Sub replaceRangeB(ByVal lo As Long, ByVal hi As Long, ByRef oldValue As String, ByRef nweValue As String)
  92.     '替换[lo..hi]对应字节
  93.     If lo < 0 Or hi >= size Or LenB(oldValue) = 0 Then
  94.         Exit Sub
  95.     End If
  96.     Dim start As Long, i As Long, oldLength As Long
  97.     Dim update As Boolean
  98.     Dim src As String
  99.     Dim sb As New StringBuilder
  100.     src = toString()
  101.     update = False
  102.     oldLength = LenB(oldValue)
  103.     i = lo + 1
  104.     start = lo + 1
  105.     Do While i <= hi + 1
  106.         i = InStrB(i, src, oldValue)
  107.         If i < 1 Then
  108.             If update Then
  109.                 sb.append MidB(src, start, hi + 2 - start)
  110.             End If
  111.             Exit Do
  112.         End If
  113.         sb.append MidB(src, start, i - start)
  114.         sb.append nweValue
  115.         i = i + oldLength
  116.         start = i
  117.         update = True
  118.     Loop
  119.     If update Then
  120.         Call removeRangeB(lo, hi)
  121.         Call insertB(sb.toString, lo)
  122.     End If
  123.     Set sb = Nothing
  124. End Sub
  125. Public Function toString() As String
  126.     '返回缓冲区中的字符串
  127.     toString = MidB(s, 1, size)
  128. End Function


  129. Private Function unsignedAdd(a As Long, b As Long) As Long
  130.    If (a Xor b) And &H80000000 Then
  131.       unsignedAdd = a + b
  132.    Else
  133.       unsignedAdd = (a Xor &H80000000) + b Xor &H80000000
  134.    End If
  135. End Function
  136. Private Sub ensureCapacity(ByVal minCapacity As Long)
  137.     If capacity >= minCapacity Then
  138.         Exit Sub
  139.     End If
  140.     Dim newCapacity As Long
  141.     newCapacity = capacity
  142.     Do While newCapacity < minCapacity
  143.         newCapacity = newCapacity + newCapacity
  144.     Loop
  145.     s = s & String((newCapacity - capacity) \ 2, ChrW(0))
  146.     capacity = newCapacity
  147. End Sub

复制代码

回复

使用道具 举报

发表于 2019-7-25 20:29:09 | 显示全部楼层
没有示例的代码,差评。
回复 赞! 靠!

使用道具 举报

 楼主| 发表于 2019-7-25 20:36:06 | 显示全部楼层
Ink_Hin_fifteen 发表于 2019-7-25 20:29
没有示例的代码,差评。

后续会添加
回复 赞! 靠!

使用道具 举报

本版积分规则

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-11-21 21:23 , Processed in 0.029921 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表