Exemple #1
0
        private static int cmp(StringCodepoints a, StringCodepoints b)
        {
            var l = Math.Min(a.utf32.Count, b.utf32.Count);

            for (var i = 0; i < l; i++)
            {
                if (a.utf32[i] < b.utf32[i])
                {
                    return(-1);
                }
                if (a.utf32[i] > b.utf32[i])
                {
                    return(1);
                }
            }
            if (a.utf32.Count > b.utf32.Count)
            {
                return(1);
            }
            if (b.utf32.Count > a.utf32.Count)
            {
                return(-1);
            }
            return(0);
        }
Exemple #2
0
        private static GraceObject mAt(EvaluationContext ctx,
                                       MethodRequest req,
                                       StringCodepoints self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var oth = req[0].Arguments[0].FindNativeParent <GraceNumber>();

            if (oth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", req.Name },
                    { "required", "Number" },
                }, "ArgumentTypeError: Index must be a number");
            }
            int idx = oth.GetInt() - 1;

            if (idx >= self.codepoints.Length || idx < 0)
            {
                ErrorReporting.RaiseError(ctx, "R2013",
                                          new Dictionary <string, string> {
                    { "index", "" + (idx + 1) },
                    { "valid", self.codepoints.Length > 0 ?
                      "1 .. " + self.codepoints.Length
                                : "none (empty)" }
                }, "IndexError: Index out of range");
            }
            if (self.codepoints[idx] == null)
            {
                self.codepoints[idx] = CodepointObject.Create(self.utf32[idx]);
            }
            return(self.codepoints[idx]);
        }
Exemple #3
0
        private static GraceObject mUTF32(EvaluationContext ctx,
                                          MethodRequest req,
                                          StringCodepoints self)
        {
            var str = makeString(self);

            return(new UTF32CodepointsView(str, self.utf32));
        }
Exemple #4
0
        private static GraceObject mUTF8(EvaluationContext ctx,
                                         MethodRequest req,
                                         StringCodepoints self)
        {
            var str = makeString(self);

            return(new ByteString(Encoding.UTF8.GetBytes(str)));
        }
Exemple #5
0
        private static GraceObject mNFD(EvaluationContext ctx,
                                        MethodRequest req,
                                        StringCodepoints self)
        {
            var str = makeString(self);

            str = str.Normalize(NormalizationForm.FormD);
            return(new StringCodepoints(str));
        }
Exemple #6
0
        private static StringCodepoints mapCodepointSequenceString(string s)
        {
            var bits = s.Split(' ');

            // Skip compatibility & formatting decompositions
            if (bits.Length >= 1 && (bits[0].Length == 0 || bits[0][0] == '<'))
            {
                bits = new String[0];
            }
            return(StringCodepoints.Create(from b in bits select
                                           Convert.ToInt32(b, 16)));
        }
Exemple #7
0
        private static GraceObject mNE(EvaluationContext ctx,
                                       MethodRequest req,
                                       StringCodepoints self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var other = req[0].Arguments[0] as StringCodepoints;

            if (other == null)
            {
                return(GraceBoolean.True);
            }
            return(GraceBoolean.Create(cmp(self, other) != 0));
        }
Exemple #8
0
        private static GraceObject mConcat(EvaluationContext ctx,
                                           MethodRequest req,
                                           StringCodepoints self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var oth = req[0].Arguments[0].FindNativeParent <StringCodepoints>();

            if (oth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", req.Name },
                    { "required", "codepoints" },
                }, "ArgumentTypeError: Needed codepoints object");
            }
            return(new StringCodepoints(self.utf32.Concat(oth.utf32)));
        }
Exemple #9
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    StringCodepoints self)
 {
     return(GraceString.Create(makeString(self)));
 }
Exemple #10
0
 private static string makeString(StringCodepoints self)
 {
     return(String.Join("",
                        from c in self.utf32
                        select Char.ConvertFromUtf32(c)));
 }
Exemple #11
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  StringCodepoints self)
 {
     return(GraceNumber.Create(self.utf32.Count));
 }