using System.Text;
namespace AIStudio.Tools.DocumentSegments;
///
/// The text of a document which is text itself, e.g., Markdown, LaTeX, or HTML, together with how it is encoded.
///
///
///
/// The new document keeps the encoding of the source, its byte order mark included, so that the programs which
/// read the source read the new document alike. Without a byte order mark, the text counts as UTF-8.
///
///
/// Reading is strict: a document whose bytes do not fit its encoding, e.g., a page in Windows-1252, or which
/// names another encoding than UTF-8 for itself, e.g., by the charset of a web page, is refused with an
/// UnsupportedTextEncodingException. Decoding it anyway would replace its characters beyond ASCII with a
/// placeholder everywhere in the file, also outside of the segments, and the new words would come out in bytes
/// which its readers take for other characters.
///
///
/// A document larger than its format reads is refused before a single byte is read, with a
/// DocumentTooLargeException, see IDocumentSegmentFormat.MaxTextSize.
///
///
/// The text of the document.
/// The encoding of the document, whose preamble is its byte order mark, if any.
public sealed record TextDocumentFile(string Text, Encoding Encoding)
{
///
/// Reads a document.
///
/// The document.
/// The size of the largest document to read, in bytes; null for no limit.
/// Finds the encoding which a text names for itself, e.g., the charset of a web page; null for a format without such a name. A byte order mark takes precedence over it, as it does for browsers.
/// Cancels the reading.
/// The document is larger than maxSize.
/// The document is not valid in its encoding, or names another encoding than UTF-8.
public static async Task ReadAsync(string path, long? maxSize, Func? declaredEncodingOf, CancellationToken token)
{
byte[] bytes;
await using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous))
{
if (maxSize is { } limit && stream.Length > limit)
throw new DocumentTooLargeException(path, stream.Length, limit);
bytes = new byte[stream.Length];
await stream.ReadExactlyAsync(bytes, token);
}
var hasByteOrderMark = ByteOrderMark.TryFind(bytes, out var markedEncoding, out var markLength);
var encoding = markedEncoding ?? ByteOrderMark.STRICT_UTF8_WITHOUT_MARK;
string text;
try
{
text = encoding.GetString(bytes, markLength, bytes.Length - markLength);
}
catch (DecoderFallbackException e)
{
throw new UnsupportedTextEncodingException(path, e);
}
if (!hasByteOrderMark && declaredEncodingOf?.Invoke(text) is { } declaredEncoding && !IsUtf8(declaredEncoding))
throw new UnsupportedTextEncodingException(path, declaredEncoding.Trim());
return new(text, encoding);
}
///
/// Writes a text with the encoding of this document.
///
///
/// The encoding throws on a character it cannot write, e.g., half of a surrogate pair, rather than writing a
/// placeholder.
///
/// The new document, which the user chose.
/// The text of the new document.
/// Cancels the writing.
public Task WriteAsync(string path, string text, CancellationToken token) => File.WriteAllTextAsync(path, text, this.Encoding, token);
///
/// Whether the name of an encoding stands for UTF-8, e.g., "UTF-8" for a web page or "utf8" for LaTeX.
///
/// The name, as a document writes it.
public static bool IsUtf8(string name)
{
var letters = new string(name.Where(char.IsAsciiLetterOrDigit).Select(char.ToLowerInvariant).ToArray());
return letters is "utf8" or "utf8x" or "unicode11utf8" or "xunicode20utf8";
}
}