Source/Common/Text/TextOffsetConversion.cs
/// <summary>
/// Returns the offset in the data string based on this text position.
/// A multi-byte character advances the offset with one.
/// A newline advances the offset with one, and offset is not affected by CR.
/// </summary>
/// <returns></returns>
Returns the offset in the data string based on this text position.
A multi-byte character advances the offset with one.
A newline advances the offset with one, and offset is not affected by CR.
Returns the offset in the data string based on this text position . A multi - byte character advances the offset with one . A newline advances the offset with one and offset is not affected by CR .
[Pure]
		public static TextOffset ToOffset(this TextPosition pos, string data)
		{
			if (string.IsNullOrEmpty(data))
				throw new ArgumentNullException(data);
			var lines = data.Split("\n");
			if (pos.Line < 1 || pos.Line > lines.Length)
				throw new IndexOutOfRangeException("Text offset is out of range");
			var sizeOfDataBefore = 0;
			for (var i = 0; i < pos.Line - 1; ++i)
			{
				sizeOfDataBefore += lines[i].Replace("\r", "").Length + 1;
			}
			var line = lines[pos.Line - 1].Replace("\r", "");
			if (pos.Character < 1 || pos.Character - 1 > line.Length)
				throw new IndexOutOfRangeException("Text offset is out of range");
			return new TextOffset(sizeOfDataBefore + pos.Character - 1);
		}
[ Pure ] public static TextOffset ToOffset ( this TextPosition pos , string data ) { if ( string . IsNullOrEmpty ( data ) ) throw new ArgumentNullException ( data ) ; var lines = data . Split ( " \n " ) ; if ( pos . Line < 1 || pos . Line > lines . Length ) throw new IndexOutOfRangeException ( " " ) ; var sizeOfDataBefore = 0 ; for ( var i = 0 ; i < pos . Line - 1 ; ++ i ) { sizeOfDataBefore += lines [ i ] . Replace ( " \r " , " " ) . Length + 1 ; } var line = lines [ pos . Line - 1 ] . Replace ( " \r " , " " ) ; if ( pos . Character < 1 || pos . Character - 1 > line . Length ) throw new IndexOutOfRangeException ( " " ) ; return new TextOffset ( sizeOfDataBefore + pos . Character - 1 ) ; }
Returns the offset in the data string based on this text position.
Returns the offset in the data string based on this text position .
None
TextPosition
this
data
string