• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

moonbitlang / x / 301

10 Dec 2024 06:19AM UTC coverage: 85.204% (-2.6%) from 87.841%
301

Pull #78

github

web-flow
Merge b830031f4 into 91f0fdf48
Pull Request #78: feat: new package encoding

105 of 161 new or added lines in 3 files covered. (65.22%)

124 existing lines in 29 files now uncovered.

1169 of 1372 relevant lines covered (85.2%)

434.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.17
/json5/lex_string.mbt
1
// Copyright 2024 International Digital Economy Academy
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
///|
16
fn lex_string(ctx : ParseContext, quote : Char) -> String!ParseError {
17
  let buf = StringBuilder::make()
15✔
18
  let mut start = ctx.offset
19
  fn flush(end : Int) {
20
    if start > 0 && end > start {
26✔
21
      buf.add_substring(ctx.input, start, end)
5✔
22
    }
23
  }
24

25
  for {
26
    match read_char(ctx) {
45✔
27
      Some('\'' | '"' as c) =>
28
        if c == quote {
8✔
29
          flush(ctx.offset - 1)
7✔
30
          break
31
        }
UNCOV
32
      Some('\n' | '\r') => invalid_char!(ctx, shift=-1)
×
33
      Some('\\') => {
19✔
34
        flush(ctx.offset - 1)
35
        match read_char(ctx) {
36
          Some('b') => buf.add_char('\b')
1✔
37
          Some('f') => buf.add_char('\x0C')
1✔
38
          Some('n') => buf.add_char('\n')
1✔
39
          Some('r') => buf.add_char('\r')
1✔
40
          Some('t') => buf.add_char('\t')
1✔
41
          Some('v') => buf.add_char('\x0B')
1✔
42
          Some('\'') => buf.add_char('\'')
1✔
43
          Some('"') => buf.add_char('"')
2✔
UNCOV
44
          Some('\\') => buf.add_char('\\')
×
45
          Some('0') => {
2✔
46
            match read_char(ctx) {
UNCOV
47
              None => ()
×
48
              Some(c) => {
2✔
49
                ctx.offset -= 1
50
                if c >= '0' && c <= '9' {
UNCOV
51
                  invalid_char!(ctx)
×
52
                }
53
              }
54
            }
55
            buf.add_char('\x00')
56
          }
57
          Some('x') => {
3✔
58
            let c = lex_hex_digits!(ctx, 2)
59
            buf.add_char(Char::from_int(c))
1✔
60
          }
61
          Some('u') => {
2✔
62
            let c = lex_hex_digits!(ctx, 4)
63
            buf.add_char(Char::from_int(c))
1✔
64
          }
65
          Some(c) => {
2✔
66
            if c >= '1' && c <= '9' {
UNCOV
67
              invalid_char!(ctx, shift=-1)
×
68
            }
69
            buf.add_char(c)
70
          }
UNCOV
71
          None => parse_error!(InvalidEof)
×
72
        }
73
        start = ctx.offset
74
      }
75
      Some(_) => continue
16✔
UNCOV
76
      None => parse_error!(InvalidEof)
×
77
    }
78
  }
79
  buf.to_string()
80
}
81

82
///|
83
fn lex_hex_digits(ctx : ParseContext, n : Int) -> Int!ParseError {
84
  let mut r = 0
12✔
85
  for i = 0; i < n; i = i + 1 {
38✔
86
    match read_char(ctx) {
41✔
87
      Some(c) =>
88
        if c >= 'A' {
41✔
89
          let d = (c.to_int() & (32).lnot()) - 'A'.to_int() + 10
8✔
90
          if d > 15 {
UNCOV
91
            invalid_char!(ctx, shift=-1)
×
92
          }
93
          r = (r << 4) | d
94
        } else if c >= '0' {
33✔
95
          let d = c.to_int() - '0'.to_int()
33✔
96
          if d > 9 {
UNCOV
97
            invalid_char!(ctx, shift=-1)
×
98
          }
99
          r = (r << 4) | d
100
        } else {
101
          invalid_char!(ctx, shift=-1)
×
102
        }
UNCOV
103
      None => parse_error!(InvalidEof)
×
104
    }
105
  }
106
  r
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc