Exemple #1
0
        static string ExpandChrSet([CanBeNull] string alphabet)
        {
            var sb = new StringBuilder(100);

            if (alphabet == null)
            {
                alphabet = "";
            }

            for (int i = 26; i > alphabet.Length; i--)
            {
                sb.Append("32,");
            }

            foreach (char c in alphabet)
            {
                sb.Append(UnicodeTranslation.ToZscii(c));
                sb.Append(',');
            }

            sb.Remove(sb.Length - 1, 1);
            return(sb.ToString());
        }
Exemple #2
0
        static string TranslateString([NotNull] ZilString zstr, [NotNull] Context ctx, char crlfChar, StringSpacesMode spacesMode)
        {
            // strip CR/LF and ensure 1 space afterward, translate crlfChar to LF,
            // and collapse two spaces after '.' or crlfChar into one
            var  sb          = new StringBuilder(zstr.Text);
            char?last        = null;
            bool sawDotSpace = false;

            var zversion = ctx.ZEnvironment.ZVersion;

            string DescribeChar(byte zscii)
            {
                switch (zscii)
                {
                case 8:
                    return("backspace");

                case 9:
                    return("tab");

                case 11:
                    return("sentence space");

                case 27:
                    return("escape");

                case var _ when zscii < 32:
                    return("^" + (char)(zscii + 64));

                case var _ when zscii < 127:
                    return("'" + (char)zscii + "'");

                default:
                    return(null);
                }
            }

            for (int i = 0; i < sb.Length; i++)
            {
                char c = sb[i];
                byte b = UnicodeTranslation.ToZscii(c);

                if (!StringEncoder.IsPrintable(b, zversion))
                {
                    var warning = new CompilerError(zstr,
                                                    CompilerMessages.ZSCII_0_1_Cannot_Be_Safely_Printed_In_Zmachine_Version_2,
                                                    b,
                                                    DescribeChar(b),
                                                    zversion);

                    ctx.HandleError(warning);
                }

                switch (spacesMode)
                {
                case StringSpacesMode.CollapseAfterPeriod:
                    if ((last == '.' || last == crlfChar) && c == ' ')
                    {
                        sawDotSpace = true;
                    }
                    else if (sawDotSpace && c == ' ')
                    {
                        sb.Remove(i--, 1);
                        sawDotSpace = false;
                        last        = c;
                        continue;
                    }
                    else
                    {
                        sawDotSpace = false;
                    }
                    break;

                case StringSpacesMode.CollapseWithSentenceSpace:
                    if ((last == '.' || last == '?' || last == '!') && c == ' ')
                    {
                        sawDotSpace = true;
                    }
                    else if (sawDotSpace && c == ' ')
                    {
                        sb.Remove(i--, 1);
                        sb[i]       = SentenceSpaceChar;
                        sawDotSpace = false;
                        last        = c;
                        continue;
                    }
                    else
                    {
                        sawDotSpace = false;
                    }
                    break;
                }

                switch (c)
                {
                case '\r':
                    sb.Remove(i--, 1);
                    continue;

                case '\n':
                    if (last == crlfChar)
                    {
                        sb.Remove(i--, 1);
                    }
                    else
                    {
                        sb[i] = ' ';
                    }
                    break;

                default:
                    if (c == crlfChar)
                    {
                        sb[i] = '\n';
                    }
                    break;
                }

                last = c;
            }

            return(sb.ToString());
        }