using System;
namespace Framework.Support {
/// Helper methods for the dialogue parser
public static class ParserHelper {
/// Returns how far the contents of a line have been indented
/// Line whose indentation will be checked
/// The indentation level of the provided line
public static int GetIndentationLevel(string line) {
if(string.IsNullOrEmpty(line)) {
return 0;
}
// Look for the first non-whitespace character, its index is the indentation level
for(int index = 0; index < line.Length; ++index) {
if(!char.IsWhiteSpace(line[index])) {
return index;
}
}
// Line consists of only whitespace
return 0;
}
/// Advances the index past any whitespace in the string
/// String which is being indexed
/// Index that will be advanced
public static void SkipSpaces(string text, ref int index) {
if(text == null) {
return;
}
int length = text.Length;
while(index < length) {
if(!char.IsWhiteSpace(text, index)) {
break;
}
++index;
}
}
/// Advances the index past any whitespace in the string
/// String which is being indexed
/// Index that will be advanced
public static void SkipAlphaNumerics(string text, ref int index) {
if(text == null) {
return;
}
int length = text.Length;
while(index < length) {
if(!char.IsLetterOrDigit(text, index)) {
break;
}
++index;
}
}
/// Advances the index past the specified character, if encountered
/// String which is being indexed
/// Index that will be advanced
/// True if the character was encountered and skipped
public static bool SkipCharacter(string text, char character, ref int index) {
if(text == null) {
return false;
}
int length = text.Length;
if(index < length) {
if(text[index] == character) {
++index;
return true;
}
}
return false;
}
/// Advances the index to the next whitespace in the string
/// String which is being indexed
/// Index that will be advanced
public static void SkipNonSpaces(string text, ref int index) {
if(text == null) {
return;
}
int length = text.Length;
while(index < length) {
if(char.IsWhiteSpace(text, index)) {
break;
}
++index;
}
}
/// Skips an integer in the provided string
/// String in which an integer will be skipped
/// Index at which the integer begins
/// True if an integer was found and skipped, otherwise false
public static bool SkipInteger(string text, ref int index) {
if(text == null) {
return false;
}
int length = text.Length;
if(index >= length) {
return false;
}
// If the number begins with a minus or plus sign, skip over the sign
if((text[index] == '-') || (text[index] == '+')) {
int nextIndex = index + 1;
if(nextIndex == length) {
return false;
}
if(!char.IsNumber(text, nextIndex)) {
return false;
}
index = nextIndex;
}
// Eat all numbers until we reach another character or the end of the string
while(char.IsNumber(text, index)) {
++index;
if(index >= length) {
break;
}
}
return true;
}
/// Locates the first non-space character in the string
/// Text in which the first non-space character will be found
/// The index of the first non-space character or -1 if none found
public static int FindNonSpace(string text) {
return FindNonSpace(text, 0);
}
/// Locates the next non-space character in the string
/// Text in which the next non-space character will be found
/// Index at which the search will begin
/// The index of the next non-space character or -1 if none found
public static int FindNonSpace(string text, int startIndex) {
while(startIndex < text.Length) {
if(!char.IsWhiteSpace(text, startIndex)) {
return startIndex;
}
++startIndex;
}
return -1;
}
/// Locates the first whitespace character in the string
/// Text in which the first whitespace character will be found
/// The index of the first whitespace character or -1 if none found
public static int FindSpace(string text) {
return FindSpace(text, 0);
}
/// Locates the next nonwhitespace character in the string
/// Text in which the next nonwhitespace character will be found
/// Index at which the search will begin
/// The index of the next whitespace character or -1 if none found
public static int FindSpace(string text, int startIndex) {
while(startIndex < text.Length) {
if(char.IsWhiteSpace(text, startIndex)) {
return startIndex;
}
++startIndex;
}
return -1;
}
}
} // namespace Framework.Support