这是以前憋得无聊做的一个(string splite)分割文本重写的算法 当然这是很简单的 纯算法我也写过现在还在邮箱不过
那个算法大量数据跑不赢.Net自带的String::Split的确对我而言是个不小得打击 不过倒也不错。
1. strtock // 分割文本 _Str 欲被分割的文本 _Delim 欲被用于分割的文本
该api貌似不能用了,智商捉急了
2: strstr // 寻找文本 _Str 欲被寻找的文本 _SubStr 欲被用于寻找的字符串
3: strlen // 取文本长度 _Str 欲被统计长度的字符串
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public static unsafe class _string
{
[DllImport("msvcr80.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string strtok(char* _Str, char* _Delim);
[DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern char* strstr(char* _Str, char* _SubStr);
[DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint strlen(char* _Str);
public static string[] Split(string value, string delim) // strtok winapi function invalid. 文本分割
{
int offset, size;
if (value != null && delim != null && (size = delim.Length) > 0) // Check string is valid.
{
fixed (char* _value = value.ToCharArray()) // Get string pointer.
{
fixed (char* _delim = delim)
{
int[] items = IndexOfs(_value, _delim, size);
string[] buffer = new string[items.Length + 0x1];
buffer[0] = new string(_value, 0, items[0x0]);
for (int i = 1; i < items.Length; i++)
{
offset = items[i - 1] + size;
buffer[i] = new string(_value, offset, items[i] - offset);
}
offset = items.Length;
buffer[offset--] = new string(_value, items[offset] + 1, value.Length - (items[offset] + 1));
return buffer;
}
}
}
return new string[0];
}
public static int[] IndexOfs(char* str, char* search, int offset) // 寻找文本s
{
char* eax = str;
List<int> items = new List<int>();
while ((str = strstr(str, search)) != null)
{
int pos;
if ((pos = (int)(str - eax)) < 0)
break;
str += offset;
items.Add(pos);
}
return items.ToArray();
}
public static int IndexOf(char* str, char* search) // 寻找文本
{
char* ptr;
if ((ptr = strstr(str, search)) != null)
return (int)(ptr - str);
return -1;
}
}