summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/ThirdParty/StringUtil/StringUtil_Other.cs
blob: d9d0616a652ebe58f3281f73358e8cfc9e850432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using UnityEngine;
using System.Collections;
using System.Text;
using System;

public partial class StringUtil
{
    /// <summary>
    /// 计算字符串的长度(包含处理中文字符)
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public static int CalcStringLetterNum(string input)
    {
        int counter = 0;
        for (int i = 0, imax = input.Length; i < imax; ++i)
        {
            if (IsChineseLetter(input, i))
            {
                counter += 2;
            }
            else
            {
                counter += 1;
            }
        }

        return counter;
    }

    /// <summary>
    /// 截断字符串(包含处理中文字符)
    /// </summary>
    /// <param name="input"></param>
    /// <param name="maxEnglishLength"></param>
    /// <returns></returns>
    public static string TrancString(string input, int maxEnglishLength)
    {
        int counter = 0;
        for (int i = 0, imax = input.Length; i < imax; ++i)
        {
            if (IsChineseLetter(input, i))
            {
                if (counter <= maxEnglishLength && maxEnglishLength < (counter + 2))
                    return input.Substring(0, i);
                counter += 2;
            }
            else
            {
                if (counter <= maxEnglishLength && maxEnglishLength < (counter + 1))
                    return input.Substring(0, i);
                counter += 1;
            }
        }

        return input;
    }

    /// <summary>
    /// 是不是中文字符
    /// </summary>
    /// <param name="input"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool IsChineseLetter(string input, int index)
    {
        int code = 0;
        int chfrom = System.Convert.ToInt32("4e00", 16);
        int chend = System.Convert.ToInt32("9fff", 16);
        if (input != "")
        {
            code = System.Char.ConvertToUtf32(input, index);

            if (code >= chfrom && code <= chend)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }

    /// <summary>
    /// 缩减字符串
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string ShrinkString(string str)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0, imax = str.Length; i < imax; i++)
        {
            if (str[i] == '\0')
                break;
            sb.Append(str[i]);
        }

        return sb.ToString();
    }

    /// <summary>
    /// 自定义的字符串比较
    /// </summary>
    /// <param name="left"></param>
    /// <param name="right"></param>
    /// <returns></returns>
    public static bool IsStringEqual(string left, string right)
    {
        if (System.Object.ReferenceEquals(left, right))
        {
            return true;
        }

        if (left == null && right == null)
            return true;
        if (left == null)
            return false;
        if (right == null)
            return false;

        int leftLen = left.Length;
        int rightLen = right.Length;
        int count = Mathf.Min(leftLen, rightLen);
        for (int index = 0; index < count; ++index)
        {
            char leftChar = left[index];
            char rightChar = right[index];
            if (leftChar != rightChar)
                return false;
        }

        if (leftLen > count && left[count] == '\0')
            return true;
        if (rightLen > count && right[count] == '\0')
            return true;
        if (leftLen == rightLen)
            return true;

        return false;
    }

    /// <summary>
    /// Bytes数组转utf8字符串
    /// </summary>
    /// <param name="buffer"></param>
    /// <param name="fromIndex"></param>
    /// <param name="count"></param>
    /// <param name="bufferSize"></param>
    /// <returns></returns>
    public static string GetUtf8StringFromByteBuffer(ref byte[] buffer, int fromIndex, int count, int bufferSize)
    {
        if (buffer == null)
            return string.Empty;
        if (fromIndex < bufferSize)
        {
            int maxCount = bufferSize - fromIndex;
            count = Mathf.Min(maxCount, count);

            string str = ShrinkString(System.Text.Encoding.UTF8.GetString(buffer, fromIndex, count));
            return str;
        }

        LogHelper.LogError("fromIndex over flow.");
        return "";
    }

    /// <summary>
    /// Bytes数组转utf8字符串
    /// </summary>
    /// <param name="buffer"></param>
    /// <returns></returns>
    public static string GetUtf8StringFromByteBuffer(ref byte[] buffer)
    {
        return GetUtf8StringFromByteBuffer(ref buffer, 0, buffer.Length, buffer.Length);
    }

    /// <summary>
    /// Utf8字符串转Byte数组
    /// </summary>
    /// <param name="srcStr"></param>
    /// <param name="srcOffset"></param>
    /// <param name="dstBuffer"></param>
    /// <param name="dstOffset"></param>
    /// <param name="dstBufferSize"></param>
    public static void CopyByteBufferFromUtf8String(string srcStr, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize)
    {
        int copyCount;
        CopyByteBufferFromUtf8String(srcStr, srcOffset, ref dstBuffer, dstOffset, dstBufferSize, out copyCount);
    }

    /// <summary>
    /// Utf8字符串转Byte数组
    /// </summary>
    /// <param name="srcStr"></param>
    /// <param name="srcOffset"></param>
    /// <param name="dstBuffer"></param>
    /// <param name="dstOffset"></param>
    /// <param name="dstBufferSize"></param>
    /// <param name="copyCount"></param>
    public static void CopyByteBufferFromUtf8String(string srcStr, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize, out int copyCount)
    {
        byte[] srcBuffer = System.Text.Encoding.UTF8.GetBytes(srcStr);
        int srcLen = srcBuffer.Length;
        srcLen = Mathf.Max(srcLen - srcOffset, 0);
        int dstMaxCopyCount = dstBufferSize - dstOffset;
        dstMaxCopyCount = Mathf.Max(dstMaxCopyCount, 0);
        dstMaxCopyCount = Mathf.Min(dstMaxCopyCount, srcLen);
        System.Buffer.BlockCopy(srcBuffer, srcOffset, dstBuffer, dstOffset, dstMaxCopyCount);

        copyCount = dstMaxCopyCount;
    }

    /// <summary>
    /// Byte数组复制
    /// </summary>
    /// <param name="srcBuffer"></param>
    /// <param name="srcOffset"></param>
    /// <param name="dstBuffer"></param>
    /// <param name="dstOffset"></param>
    /// <param name="dstBufferSize"></param>
    public static void CopyByteBuffer(ref byte[] srcBuffer, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize)
    {
        int copyCount;
        CopyByteBuffer(ref srcBuffer, srcOffset, ref dstBuffer, dstOffset, dstBufferSize, out copyCount);
    }

    /// <summary>
    /// Byte数组复制
    /// </summary>
    /// <param name="srcBuffer"></param>
    /// <param name="srcOffset"></param>
    /// <param name="dstBuffer"></param>
    /// <param name="dstOffset"></param>
    /// <param name="dstBufferSize"></param>
    /// <param name="copyCount"></param>
    public static void CopyByteBuffer(ref byte[] srcBuffer, int srcOffset, ref byte[] dstBuffer, int dstOffset, int dstBufferSize, out int copyCount)
    {
        int srcLen = srcBuffer.Length;
        srcLen = Mathf.Max(srcLen - srcOffset, 0);
        int dstMaxCopyCount = dstBufferSize - dstOffset;
        dstMaxCopyCount = Mathf.Max(dstMaxCopyCount, 0);
        dstMaxCopyCount = Mathf.Min(dstMaxCopyCount, srcLen);
        System.Buffer.BlockCopy(srcBuffer, srcOffset, dstBuffer, dstOffset, dstMaxCopyCount);

        copyCount = dstMaxCopyCount;
    }

    /// <summary>
    /// Byte数组转十六进制字符串
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static string BytesToHex(byte[] bytes)
    {
        char[] c = new char[bytes.Length * 2];

        byte b;

        for (int bx = 0, cx = 0; bx < bytes.Length; ++bx, ++cx)
        {
            b = ((byte)(bytes[bx] >> 4));
            c[cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30);

            b = ((byte)(bytes[bx] & 0x0F));
            c[++cx] = (char)(b > 9 ? b + 0x37 + 0x20 : b + 0x30);
        }

        return new string(c);
    }

    /// <summary>
    /// 十六进制字符串转Byte数组
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static byte[] HexToBytes(string str)
    {
        if (str.Length == 0 || str.Length % 2 != 0)
            return new byte[0];

        byte[] buffer = new byte[str.Length / 2];
        char c;
        for (int bx = 0, sx = 0; bx < buffer.Length; ++bx, ++sx)
        {
            // Convert first half of byte
            c = str[sx];
            buffer[bx] = (byte)((c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')) << 4);

            // Convert second half of byte
            c = str[++sx];
            buffer[bx] |= (byte)(c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0'));
        }

        return buffer;
    }

}