using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
namespace Tessera.UnitTests.Unicode
{
///
/// Reads the Unicode Consortium's boundary conformance files, LineBreakTest.txt, GraphemeBreakTest.txt and
/// WordBreakTest.txt, which the build unpacks next to this binary from 3rdParty\Unicode.
///
public static class BoundaryTestFile
{
public const char BreakMark = (char)0x00F7;
public struct Boundary
{
///
/// UTF-16 offset into the case's text.
///
public int Index;
public bool Breaks;
}
///
/// One line of a file: its text, and the verdict on every boundary strictly inside it.
///
public class Case
{
public string Text;
public List Boundaries = new List();
}
public static string PathOf(string fileName)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Unicode", fileName);
}
public static Case Parse(string line)
{
int commentStart = line.IndexOf('#');
string[] tokens = line.Substring(0, commentStart)
.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
// Tokens alternate verdict, code point, verdict, ..., verdict. The first and last verdicts are the start
// and end of text, where a break is a given.
var text = new StringBuilder();
var returnMe = new Case();
for (int i = 0; i < tokens.Length; i += 2)
{
if (i > 0 && i < tokens.Length - 1)
{
returnMe.Boundaries.Add(new Boundary
{
Index = text.Length,
Breaks = tokens[i][0] == BreakMark,
});
}
if (i + 1 < tokens.Length)
{
text.Append(char.ConvertFromUtf32(int.Parse(tokens[i + 1], NumberStyles.HexNumber)));
}
}
returnMe.Text = text.ToString();
return returnMe;
}
///
/// Returns every case in a file, skipping its comments and blank lines.
///
public static Case[] ReadCases(string fileName)
{
var returnMe = new List();
foreach (string line in File.ReadLines(PathOf(fileName)))
{
if (line.Length == 0 || line[0] == '#')
{
continue;
}
returnMe.Add(Parse(line));
}
return returnMe.ToArray();
}
}
}