Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StringSpan.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Runtime.InteropServices;
5 
6 namespace SiliconStudio.Core
7 {
8  /// <summary>
9  /// A region of character in a string.
10  /// </summary>
11  [StructLayout(LayoutKind.Sequential)]
12  public struct StringSpan : IEquatable<StringSpan>
13  {
14  /// <summary>
15  /// Initializes a new instance of the <see cref="StringSpan"/> struct.
16  /// </summary>
17  /// <param name="start">The start.</param>
18  /// <param name="length">The length.</param>
19  public StringSpan(int start, int length)
20  {
21  Start = start;
22  Length = length;
23  }
24 
25  /// <summary>
26  /// The start of the span.
27  /// </summary>
28  public int Start;
29 
30  /// <summary>
31  /// The length of the span
32  /// </summary>
33  public int Length;
34 
35  /// <summary>
36  /// Gets a value indicating whether this instance is valid (Start greater or equal to 0, and Length greater than 0)
37  /// </summary>
38  /// <value><c>true</c> if this instance is valid; otherwise, <c>false</c>.</value>
39  public bool IsValid
40  {
41  get
42  {
43  return Start >= 0 && Length > 0;
44  }
45  }
46 
47  /// <summary>
48  /// Gets the next position = Start + Length.
49  /// </summary>
50  /// <value>The next.</value>
51  public int Next
52  {
53  get
54  {
55  return Start + Length;
56  }
57  }
58 
59  public bool Equals(StringSpan other)
60  {
61  return Start == other.Start && Length == other.Length;
62  }
63 
64  public override bool Equals(object obj)
65  {
66  if (ReferenceEquals(null, obj)) return false;
67  return obj is StringSpan && Equals((StringSpan)obj);
68  }
69 
70  public override int GetHashCode()
71  {
72  unchecked
73  {
74  return (Start*397) ^ Length;
75  }
76  }
77 
78  public static bool operator ==(StringSpan left, StringSpan right)
79  {
80  return left.Equals(right);
81  }
82 
83  public static bool operator !=(StringSpan left, StringSpan right)
84  {
85  return !left.Equals(right);
86  }
87 
88  public override string ToString()
89  {
90  return IsValid ? string.Format("[{0}-{1}]", Start, Next - 1) : "[N/A]";
91  }
92  }
93 }
_Use_decl_annotations_ bool IsValid(DXGI_FORMAT fmt)
Definition: DirectXTex.inl:25
StringSpan(int start, int length)
Initializes a new instance of the StringSpan struct.
Definition: StringSpan.cs:19
override int GetHashCode()
Definition: StringSpan.cs:70
override string ToString()
Definition: StringSpan.cs:88
int Start
The start of the span.
Definition: StringSpan.cs:28
override bool Equals(object obj)
Definition: StringSpan.cs:64
bool Equals(StringSpan other)
Definition: StringSpan.cs:59
int Length
The length of the span
Definition: StringSpan.cs:33
A region of character in a string.
Definition: StringSpan.cs:12