From 257ba1ab1799d56936f4187824703fb0a414ace4 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Wed, 5 Oct 2022 07:45:50 +1100 Subject: [PATCH] Capitalise first char of line --- 70_Poetry/csharp/Context.cs | 17 ++++++++++++++--- 70_Poetry/csharp/Phrase.cs | 2 +- 70_Poetry/csharp/Poem.cs | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/70_Poetry/csharp/Context.cs b/70_Poetry/csharp/Context.cs index 950c7c53..a9297915 100644 --- a/70_Poetry/csharp/Context.cs +++ b/70_Poetry/csharp/Context.cs @@ -9,6 +9,7 @@ internal class Context private bool _skipComma; private int _lineCount; private bool _useGroup2; + private bool _atStartOfLine = true; public Context(IReadWrite io, IRandom random) { @@ -34,6 +35,7 @@ internal class Context public void WritePhrase() { Phrase.GetPhrase(this).Write(_io, this); + _atStartOfLine = false; } public void MaybeWriteComma() @@ -55,7 +57,7 @@ internal class Context } else { - _io.WriteLine(); + EndLine(); PhraseCount = 0; } } @@ -75,10 +77,10 @@ internal class Context } } - public void ResetGroup(IReadWrite io) + public void ResetGroup() { _groupNumber = 0; - io.WriteLine(); + EndLine(); } public bool MaybeCompleteStanza() @@ -94,5 +96,14 @@ internal class Context return false; } + internal string MaybeCapitalise(string text) => + _atStartOfLine ? (char.ToUpper(text[0]) + text[1..]) : text; + public void SkipNextComma() => _skipComma = true; + + public void EndLine() + { + _io.WriteLine(); + _atStartOfLine = true; + } } diff --git a/70_Poetry/csharp/Phrase.cs b/70_Poetry/csharp/Phrase.cs index c1ac0866..f70de30b 100644 --- a/70_Poetry/csharp/Phrase.cs +++ b/70_Poetry/csharp/Phrase.cs @@ -70,7 +70,7 @@ internal class Phrase { if (_condition.Invoke(context)) { - io.Write(_text); + io.Write(context.MaybeCapitalise(_text)); } _update.Invoke(context); diff --git a/70_Poetry/csharp/Poem.cs b/70_Poetry/csharp/Poem.cs index 03f3af68..fa3d5045 100644 --- a/70_Poetry/csharp/Poem.cs +++ b/70_Poetry/csharp/Poem.cs @@ -23,7 +23,7 @@ internal class Poem if (context.GroupNumberIsValid) { break; } - context.ResetGroup(io); + context.ResetGroup(); if (context.MaybeCompleteStanza()) { break; } }