using System; using System.Collections.Generic; namespace ProtoCSStruct { public class FixedStringWriter { public FixedStringWriter() { } public void Write(int length, FileWriter fw) { string classname = FieldTypeUtils.GetFixedStringClassName(length); fw.Write( "unsafe public struct " + classname, "{" ); fw.Indent(); fw.Write( "public const int BuffSize = " + length.ToString() + ";", "public int Count;", "public fixed char m_buff[BuffSize];", ""); WriteFun_Constructor(classname, fw); WriteFun_Length(fw); WriteFun_IsEmpty(fw); WriteFun_MaxLength(fw); WriteFun_Clear(fw); WriteFun_Get(classname,fw); WriteFun_GetString(fw); WriteFun_SetString(fw); WriteFun_CopyFrom(classname, fw); WriteFun_ToString(fw); WriteFun_GetPtr(fw); WriteFun_Equals(fw); WriteFun_CalculateSize(fw); WriteFun_WriteTo(fw); WriteFun_ReadFrom(fw); fw.Outdent(); fw.Write("}"); //class fw.Write(""); } private void WriteFun_Constructor(string classname, FileWriter fw) { fw.Write( "public " + classname + "(string value)", "{", " Count = 0;", " SetString(value);", "}", "" ); } private void WriteFun_Length(FileWriter fw) { fw.Write( "public int Length => Count;", "" ); } private void WriteFun_IsEmpty(FileWriter fw) { fw.Write( "public bool IsEmpty()", "{", " return Count == 0;", "}", "" ); } private void WriteFun_MaxLength(FileWriter fw) { fw.Write( "public int MaxLength()", "{", " return BuffSize;", "}", "" ); } private void WriteFun_Clear(FileWriter fw) { fw.Write( "public void Clear()", "{", " if (Count != 0)", " {", " Count = 0;", " }", "}", "" ); } private void WriteFun_Get(string classname,FileWriter fw) { fw.Write( "public ref char this[int index]", "{", " get", " {", " if (index < 0 || index >= Count)", " {", " string message = string.Format(\"class " + classname + " index{0} count{1}\", index, Count);", " throw new ArgumentOutOfRangeException(message);", " }", " fixed (char* p = m_buff)", " {", " return ref p[index];", " }", " }", "}", "" ); } private void WriteFun_GetString(FileWriter fw) { fw.Write( "public string GetString()", "{", " StringBuilder sb = new StringBuilder(Count+4);", " fixed (char* p = m_buff)", " {", " sb.Append(p,Count);", " }", " return sb.ToString();", "}", "" ); } private void WriteFun_SetString(FileWriter fw) { fw.Write( "public void SetString(string value)", "{", " Count = 0;", " if (string.IsNullOrEmpty(value))", " {", " return;", " }", " fixed (char* p = m_buff)", " {", " for ( int i = 0; i < value.Length && i < BuffSize; i++)", " {", " p[i] = value[i];", " }", " Count = value.Length < BuffSize ? value.Length : BuffSize;", " }", "}", "" ); fw.Write( "public void SetString(Span value)", "{", " Count = 0;", " if (value.Length == 0)", " {", " return;", " }", " fixed (char* p = m_buff)", " {", " for ( int i = 0; i < value.Length && i < BuffSize; i++)", " {", " p[i] = value[i];", " }", " Count = value.Length < BuffSize ? value.Length : BuffSize;", " }", "}", "" ); } private void WriteFun_CopyFrom(string classname, FileWriter fw) { fw.Write( "public void CopyFrom(ref " + classname + " other)", "{", " SetString(other.GetPtr());", "}", "" ); } private void WriteFun_ToString(FileWriter fw) { fw.Write( "public override string ToString()", "{", " return GetString();", "}", "" ); } private void WriteFun_GetPtr(FileWriter fw) { fw.Write( "public Span GetPtr()", "{", " fixed (char* p = m_buff)", " {", " return new Span(p, Count);", " }", "}", "" ); } private void WriteFun_Equals(FileWriter fw) { fw.Write( "public bool Equals(string value)", "{", " if (value == null)", " {", " return Count == 0;", " }", " if (value.Length != Count)", " {", " return false;", " }", " fixed (char* p = m_buff)", " {", " for (int i = 0; i < value.Length; i++)", " {", " if (p[i] != value[i])", " {", " return false;", " }", " }", " }", " return true;", "}", "" ); fw.Write( "public bool Equals(Span value)", "{", " if (value.Length != Count)", " {", " return false;", " }", " fixed (char* p = m_buff)", " {", " for (int i = 0; i < Count; i++)", " {", " if (p[i] != value[i])", " {", " return false;", " }", " }", " }", " return true;", "}", "" ); fw.Write( "[Obsolete(\"Shield base object ValueType.Equals method, ValueType.Equals return is wrong\", true)]", "new public bool Equals(object obj)", "{", " string message = string.Format(\"Method Equals(object obj) forbidden!!!\");", " throw new ArgumentOutOfRangeException(message);", "}" ); } private void WriteFun_CalculateSize(FileWriter fw) { fw.Write( "public int CalculateSize()", "{", " fixed (char* p = m_buff) ", " {", " return CodedOutputStream.Utf8Encoding.GetByteCount(p,Count);", " }", "}" ); } private void WriteFun_WriteTo(FileWriter fw) { fw.Write( "public void WriteTo(CodedOutputStream output)", "{", " fixed (char* p = m_buff) ", " {", " output.WriteFixedStructString(p,Count);", " }", "}" ); } private void WriteFun_ReadFrom(FileWriter fw) { fw.Write( "public void ReadFrom(CodedInputStream input)", "{", " fixed (char* p = m_buff) ", " {", " Count = input.ReadFixedStructString(p,BuffSize);", " }", "}" ); } } }