internal static int x(HeaderQueryParser parser, ref valueType value, byte *names) { int index = 0; while ((names = parser.IsName(names, ref index)) != null) { if (index == -1) { return(-1); } memberParsers[index](parser, ref value); ++index; } return(index); }
/// <summary> /// 对象解析 /// </summary> /// <param name="parser">查询解析器</param> /// <param name="value">目标数据</param> internal static void Parse(HeaderQueryParser parser, ref valueType value) { byte *names = memberNames.Byte; int index = 0; while ((names = parser.IsName(names, ref index)) != null) { if (index == -1) { return; } memberParsers[index](parser, ref value); ++index; } do { if ((index = memberSearcher.SearchName(parser)) != -1) { memberParsers[index](parser, ref value); } }while (parser.IsQuery()); }
/// <summary> /// 获取名称索引 /// </summary> /// <param name="parser">查询解析器</param> /// <returns>名称索引,失败返回-1</returns> internal int SearchName(HeaderQueryParser parser) { if (state == null) { return(-1); } byte value = parser.GetName(); if (value == 0) { return(*(int *)(state + sizeof(int) * 2)); } byte *currentState = state; do { char *prefix = (char *)(currentState + *(int *)currentState); if (*prefix != 0) { if (value != *prefix) { return(-1); } while (*++prefix != 0) { if (parser.GetName() != *prefix) { return(-1); } } value = parser.GetName(); } if (value == 0) { return(*(int *)(currentState + sizeof(int) * 2)); } if (*(int *)(currentState + sizeof(int)) == 0 || value >= 128) { return(-1); } int index = (int)*(ushort *)(charsAscii + (value << 1)); byte *table = currentState + *(int *)(currentState + sizeof(int)); if (tableType == 0) { if ((index = *(table + index)) == 0) { return(-1); } currentState = state + index * 3 * sizeof(int); } else if (tableType == 1) { if ((index = (int)*(ushort *)(table + index * sizeof(ushort))) == 0) { return(-1); } currentState = state + index * 3 * sizeof(int); } else { if ((index = *(int *)(table + index * sizeof(int))) == 0) { return(-1); } currentState = state + index; } value = parser.GetName(); }while (true); }
static HeaderQueryTypeParser() { Type type = typeof(valueType); AutoCSer.Json.ParseAttribute attribute = TypeAttribute.GetAttribute <AutoCSer.Json.ParseAttribute>(type, true) ?? AutoCSer.Json.Parser.AllMemberAttribute; FieldIndex defaultMember = null; LeftArray <FieldIndex> fields = AutoCSer.Json.ParseMethodCache.GetFields(MemberIndexGroup <valueType> .GetFields(attribute.MemberFilters), attribute, ref defaultMember); LeftArray <KeyValue <PropertyIndex, MethodInfo> > properties = AutoCSer.Json.ParseMethodCache.GetProperties(MemberIndexGroup <valueType> .GetProperties(attribute.MemberFilters), attribute); memberParsers = new TryParse[fields.Length + properties.Length + (defaultMember == null ? 0 : 1)]; string[] names = new string[memberParsers.Length]; int index = 0, nameLength = 0, maxNameLength = 0; foreach (FieldIndex member in fields) { #if NOJIT TryParse tryParse = new HeaderQueryFieldParser(member.Member).Parser(); #else ILGenerator generator; DynamicMethod memberDynamicMethod = HeaderQueryParser.CreateDynamicMethod(type, member.Member.Name, member.Member.FieldType, out generator); generator.Emit(OpCodes.Stfld, member.Member); generator.Emit(OpCodes.Ret); TryParse tryParse = (TryParse)memberDynamicMethod.CreateDelegate(typeof(TryParse)); #endif memberParsers[index] = tryParse; if (member.Member.Name.Length > maxNameLength) { maxNameLength = member.Member.Name.Length; } nameLength += (names[index++] = member.Member.Name).Length; if (member == defaultMember) { memberParsers[names.Length - 1] = tryParse; names[names.Length - 1] = string.Empty; } } foreach (KeyValue <PropertyIndex, MethodInfo> member in properties) { #if NOJIT memberParsers[index] = new HeaderQueryPropertyParser(member.Key.Member).Parser(); #else ILGenerator generator; DynamicMethod memberDynamicMethod = HeaderQueryParser.CreateDynamicMethod(type, member.Key.Member.Name, member.Key.Member.PropertyType, out generator); generator.call(member.Value); generator.Emit(OpCodes.Ret); memberParsers[index] = (TryParse)memberDynamicMethod.CreateDelegate(typeof(TryParse)); #endif if (member.Key.Member.Name.Length > maxNameLength) { maxNameLength = member.Key.Member.Name.Length; } nameLength += (names[index++] = member.Key.Member.Name).Length; } if (maxNameLength > short.MaxValue || nameLength == 0) { memberNames = Unmanaged.NullByte8; } else { memberNames = new Pointer { Data = Unmanaged.GetStatic(nameLength + (names.Length - (defaultMember == null ? 0 : 1)) * sizeof(short) + sizeof(short), false) }; byte *write = memberNames.Byte; foreach (string name in names) { if (name.Length != 0) { *(short *)write = (short)name.Length; fixed(char *nameFixed = name) StringExtension.WriteBytesNotNull(nameFixed, name.Length, write + sizeof(short)); write += sizeof(short) + name.Length; } } *(short *)write = 0; } memberSearcher = new HeaderQueryParseStateSearcher(AutoCSer.Json.StateSearcher.GetMemberSearcher(type, names)); }