-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
inline.go
749 lines (699 loc) · 19.2 KB
/
inline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package md
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
// InlineOp represents an inline operation.
type InlineOp struct {
Type InlineOpType
// OpText, OpCodeSpan, OpRawHTML, OpAutolink: Text content
// OpLinkStart, OpLinkEnd, OpImage: title text
Text string
// OpLinkStart, OpLinkEnd, OpImage, OpAutolink
Dest string
// ForOpImage
Alt string
}
// InlineOpType enumerates possible types of an InlineOp.
type InlineOpType uint
const (
// Text elements. Embedded newlines in OpText are turned into OpNewLine, but
// OpRawHTML can contain embedded newlines. OpCodeSpan never contains
// embedded newlines.
OpText InlineOpType = iota
OpCodeSpan
OpRawHTML
OpNewLine
// Inline markup elements.
OpEmphasisStart
OpEmphasisEnd
OpStrongEmphasisStart
OpStrongEmphasisEnd
OpLinkStart
OpLinkEnd
OpImage
OpAutolink
OpHardLineBreak
)
// String returns the text content of the InlineOp
func (op InlineOp) String() string {
switch op.Type {
case OpText, OpCodeSpan, OpRawHTML, OpAutolink:
return op.Text
case OpNewLine:
return "\n"
case OpImage:
return op.Alt
}
return ""
}
func renderInline(text string) []InlineOp {
p := inlineParser{text, 0, makeDelimStack(), buffer{}}
p.render()
return p.buf.ops()
}
type inlineParser struct {
text string
pos int
delims delimStack
buf buffer
}
func (p *inlineParser) render() {
for p.pos < len(p.text) {
b := p.text[p.pos]
begin := p.pos
p.pos++
parseText := func() {
for p.pos < len(p.text) && !isMeta(p.text[p.pos]) {
p.pos++
}
text := p.text[begin:p.pos]
hardLineBreak := false
if p.pos < len(p.text) && p.text[p.pos] == '\n' {
// https://spec.commonmark.org/0.30/#hard-line-break
//
// The input to renderInline never ends in a newline, so all
// newlines are internal ones, thus subject to the hard line
// break rules
hardLineBreak = strings.HasSuffix(text, " ")
text = strings.TrimRight(text, " ")
}
p.buf.push(textPiece(text))
if hardLineBreak {
p.buf.push(piece{main: InlineOp{Type: OpHardLineBreak}})
}
}
switch b {
// The 3 branches below implement the first part of
// https://spec.commonmark.org/0.30/#an-algorithm-for-parsing-nested-emphasis-and-links.
case '[':
bufIdx := p.buf.push(textPiece("["))
p.delims.push(&delim{typ: '[', bufIdx: bufIdx})
case '!':
if p.pos < len(p.text) && p.text[p.pos] == '[' {
p.pos++
bufIdx := p.buf.push(textPiece("!["))
p.delims.push(&delim{typ: '!', bufIdx: bufIdx})
} else {
parseText()
}
case '*', '_':
p.consumeRun(b)
canOpen, canClose := canOpenCloseEmphasis(rune(b),
emptyToNewline(utf8.DecodeLastRuneInString(p.text[:begin])),
emptyToNewline(utf8.DecodeRuneInString(p.text[p.pos:])))
bufIdx := p.buf.push(textPiece(p.text[begin:p.pos]))
p.delims.push(
&delim{typ: b, bufIdx: bufIdx,
n: p.pos - begin, canOpen: canOpen, canClose: canClose})
case ']':
// https://spec.commonmark.org/0.30/#look-for-link-or-image.
var opener *delim
for d := p.delims.top.prev; d != p.delims.bottom; d = d.prev {
if d.typ == '[' || d.typ == '!' {
opener = d
break
}
}
if opener == nil || opener.inactive {
if opener != nil {
unlink(opener)
}
p.buf.push(textPiece("]"))
continue
}
n, dest, title := parseLinkTail(p.text[p.pos:])
if n == -1 {
unlink(opener)
p.buf.push(textPiece("]"))
continue
}
p.pos += n
p.processEmphasis(opener)
if opener.typ == '[' {
for d := opener.prev; d != p.delims.bottom; d = d.prev {
if d.typ == '[' {
d.inactive = true
}
}
}
unlink(opener)
if opener.typ == '[' {
p.buf.pieces[opener.bufIdx] = piece{
before: []InlineOp{{Type: OpLinkStart, Dest: dest, Text: title}}}
p.buf.push(piece{
after: []InlineOp{{Type: OpLinkEnd, Dest: dest, Text: title}}})
} else {
// Use the pieces after "![" to build the image alt text.
var altBuilder strings.Builder
for _, piece := range p.buf.pieces[opener.bufIdx+1:] {
altBuilder.WriteString(piece.main.String())
}
p.buf.pieces = p.buf.pieces[:opener.bufIdx]
alt := altBuilder.String()
p.buf.push(piece{
main: InlineOp{Type: OpImage, Dest: dest, Alt: alt, Text: title}})
}
case '`':
// https://spec.commonmark.org/0.30/#code-spans
p.consumeRun('`')
closer := findBacktickRun(p.text, p.text[begin:p.pos], p.pos)
if closer == -1 {
// No matching closer, don't parse as code span.
parseText()
continue
}
p.buf.push(piece{
main: InlineOp{Type: OpCodeSpan,
Text: normalizeCodeSpanContent(p.text[p.pos:closer])}})
p.pos = closer + (p.pos - begin)
case '<':
// https://spec.commonmark.org/0.30/#raw-html
if p.pos == len(p.text) {
parseText()
continue
}
parseWithRegexp := func(pattern *regexp.Regexp) bool {
html := pattern.FindString(p.text[begin:])
if html == "" {
return false
}
p.buf.push(htmlPiece(html))
p.pos = begin + len(html)
return true
}
parseWithCloser := func(closer string) bool {
i := strings.Index(p.text[p.pos:], closer)
if i == -1 {
return false
}
p.pos += i + len(closer)
p.buf.push(htmlPiece(p.text[begin:p.pos]))
return true
}
switch p.text[p.pos] {
case '!':
switch {
case strings.HasPrefix(p.text[p.pos:], "!--"):
// Try parsing a comment.
if parseWithCloser("-->") {
continue
}
case strings.HasPrefix(p.text[p.pos:], "![CDATA["):
// Try parsing a CDATA section
if parseWithCloser("]]>") {
continue
}
case p.pos+1 < len(p.text) && isASCIILetter(p.text[p.pos+1]):
// Try parsing a declaration.
if parseWithCloser(">") {
continue
}
}
case '?':
// Try parsing a processing instruction.
closer := strings.Index(p.text[p.pos:], "?>")
if closer != -1 {
p.buf.push(htmlPiece(p.text[begin : p.pos+closer+2]))
p.pos += closer + 2
continue
}
case '/':
// Try parsing a closing tag.
if parseWithRegexp(closingTagRegexp) {
continue
}
default:
// Try parsing a open tag.
if parseWithRegexp(openTagRegexp) {
continue
} else {
// Try parsing an autolink.
autolink := uriAutolinkRegexp.FindString(p.text[begin:])
email := false
if autolink == "" {
autolink = emailAutolinkRegexp.FindString(p.text[begin:])
email = true
}
if autolink != "" {
p.pos = begin + len(autolink)
// Autolinks support character references but not
// backslashes, so UnescapeHTML gives us the desired
// behavior.
text := UnescapeHTML(autolink[1 : len(autolink)-1])
dest := text
if email {
dest = "mailto:" + dest
}
p.buf.push(piece{
main: InlineOp{Type: OpAutolink, Text: text, Dest: dest},
})
continue
}
}
}
parseText()
case '&':
// https://spec.commonmark.org/0.30/#entity-and-numeric-character-references
if entity := leadingCharRef(p.text[begin:]); entity != "" {
p.buf.push(textPiece(UnescapeHTML(entity)))
p.pos = begin + len(entity)
} else {
parseText()
}
case '\\':
// https://spec.commonmark.org/0.30/#backslash-escapes
if p.pos < len(p.text) {
if p.text[p.pos] == '\n' {
// https://spec.commonmark.org/0.30/#hard-line-break
//
// Do *not* consume the newline; "\\\n" is a hard line break
// plus a (soft) line break.
p.buf.push(piece{main: InlineOp{Type: OpHardLineBreak}})
continue
} else if isASCIIPunct(p.text[p.pos]) {
// Valid backslash escape: handle this by just discarding
// the backslash. The parseText call below will consider the
// next byte to be already included in the text content.
begin++
p.pos++
}
}
parseText()
case '\n':
// Hard line breaks are already inserted using lookahead in
// parseText and the case '\\' branch.
p.buf.push(piece{main: InlineOp{Type: OpNewLine}})
// Remove spaces at the beginning of the next line per
// https://spec.commonmark.org/0.30/#soft-line-breaks.
for p.pos < len(p.text) && p.text[p.pos] == ' ' {
p.pos++
}
default:
parseText()
}
}
p.processEmphasis(p.delims.bottom)
}
func (p *inlineParser) consumeRun(b byte) {
for p.pos < len(p.text) && p.text[p.pos] == b {
p.pos++
}
}
// Processes the (rune, int) result of utf8.Decode* so that an empty result is
// converted to '\n'.
func emptyToNewline(r rune, l int) rune {
if l == 0 {
return '\n'
}
return r
}
// Returns whether an emphasis punctuation can open or close an emphasis, when
// following prev and preceding next. Start and end of file should be
// represented by '\n'.
//
// The criteria are described in:
// https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis
//
// The algorithm is a bit complicated. Here is another way to describe the
// criteria:
//
// - Every rune falls into one of three categories: space, punctuation and
// other. "Other" is the category of word runes in "intraword emphasis".
//
// - The following tables describe whether a punctuation can open or close
// emphasis:
//
// Can open emphasis:
//
// | | next space | next punct | next other |
// | ---------- | ---------- | ---------- | ---------- |
// | prev space | | _ or * | _ or * |
// | prev punct | | _ or * | _ or * |
// | prev other | | | only * |
//
// Can close emphasis:
//
// | | next space | next punct | next other |
// | ---------- | ---------- | ---------- | ---------- |
// | prev space | | | |
// | prev punct | _ or * | _ or * | |
// | prev other | _ or * | _ or * | only * |
func canOpenCloseEmphasis(b, prev, next rune) (bool, bool) {
leftFlanking := !unicode.IsSpace(next) &&
(!isUnicodePunct(next) || unicode.IsSpace(prev) || isUnicodePunct(prev))
rightFlanking := !unicode.IsSpace(prev) &&
(!isUnicodePunct(prev) || unicode.IsSpace(next) || isUnicodePunct(next))
if b == '*' {
return leftFlanking, rightFlanking
}
return leftFlanking && (!rightFlanking || isUnicodePunct(prev)),
rightFlanking && (!leftFlanking || isUnicodePunct(next))
}
// Returns the starting index of the next backtick run identical to the given
// run, starting from i. Returns -1 if no such run exists.
func findBacktickRun(s, run string, i int) int {
for i < len(s) {
j := strings.Index(s[i:], run)
if j == -1 {
return -1
}
j += i
if j+len(run) == len(s) || s[j+len(run)] != '`' {
return j
}
// Too many backticks; skip over the entire run.
for j += len(run); j < len(s) && s[j] == '`'; j++ {
}
i = j
}
return -1
}
func normalizeCodeSpanContent(s string) string {
s = strings.ReplaceAll(s, "\n", " ")
if len(s) > 1 && s[0] == ' ' && s[len(s)-1] == ' ' && strings.Trim(s, " ") != "" {
return s[1 : len(s)-1]
}
return s
}
// https://spec.commonmark.org/0.30/#process-emphasis
func (p *inlineParser) processEmphasis(bottom *delim) {
var openersBottom [2][3][2]*delim
for closer := bottom.next; closer != nil; {
if !closer.canClose {
closer = closer.next
continue
}
openerBottom := &openersBottom[b2i(closer.typ == '_')][closer.n%3][b2i(closer.canOpen)]
if *openerBottom == nil {
*openerBottom = bottom
}
var opener *delim
for p := closer.prev; p != *openerBottom && p != bottom; p = p.prev {
if p.canOpen && p.typ == closer.typ &&
((!p.canClose && !closer.canOpen) ||
(p.n+closer.n)%3 != 0 || (p.n%3 == 0 && closer.n%3 == 0)) {
opener = p
break
}
}
if opener == nil {
*openerBottom = closer.prev
if !closer.canOpen {
closer.prev.next = closer.next
closer.next.prev = closer.prev
}
closer = closer.next
continue
}
openerPiece := &p.buf.pieces[opener.bufIdx]
closerPiece := &p.buf.pieces[closer.bufIdx]
strong := len(openerPiece.main.Text) >= 2 && len(closerPiece.main.Text) >= 2
if strong {
openerPiece.main.Text = openerPiece.main.Text[2:]
openerPiece.append(InlineOp{Type: OpStrongEmphasisStart})
closerPiece.main.Text = closerPiece.main.Text[2:]
closerPiece.prepend(InlineOp{Type: OpStrongEmphasisEnd})
} else {
openerPiece.main.Text = openerPiece.main.Text[1:]
openerPiece.append(InlineOp{Type: OpEmphasisStart})
closerPiece.main.Text = closerPiece.main.Text[1:]
closerPiece.prepend(InlineOp{Type: OpEmphasisEnd})
}
opener.next = closer
closer.prev = opener
if openerPiece.main.Text == "" {
opener.prev.next = opener.next
opener.next.prev = opener.prev
}
if closerPiece.main.Text == "" {
closer.prev.next = closer.next
closer.next.prev = closer.prev
closer = closer.next
}
}
bottom.next = p.delims.top
p.delims.top.prev = bottom
}
func b2i(b bool) int {
if b {
return 1
} else {
return 0
}
}
// Stores output of inline rendering.
type buffer struct {
pieces []piece
}
func (b *buffer) push(p piece) int {
b.pieces = append(b.pieces, p)
return len(b.pieces) - 1
}
func (b *buffer) ops() []InlineOp {
var ops []InlineOp
for _, p := range b.pieces {
p.iterate(func(op InlineOp) {
if op.Type == OpText {
// Convert any embedded newlines into OpNewLine, and merge
// adjacent OpText's or OpRawHTML's.
if op.Text == "" {
return
}
lines := strings.Split(op.Text, "\n")
if len(ops) > 0 && ops[len(ops)-1].Type == op.Type {
ops[len(ops)-1].Text += lines[0]
} else if lines[0] != "" {
ops = append(ops, InlineOp{Type: op.Type, Text: lines[0]})
}
for _, line := range lines[1:] {
ops = append(ops, InlineOp{Type: OpNewLine})
if line != "" {
ops = append(ops, InlineOp{Type: op.Type, Text: line})
}
}
} else {
ops = append(ops, op)
}
})
}
return ops
}
// The algorithm described in
// https://spec.commonmark.org/0.30/#phase-2-inline-structure involves inserting
// nodes before and after existing nodes in the output. The most natural choice
// is a doubly linked list; but for simplicity, we use a slice for output nodes,
// keep track of nodes that need to be prepended or appended to each node.
//
// TODO: Compare the performance of this data structure with doubly linked
// lists.
type piece struct {
before []InlineOp
main InlineOp
after []InlineOp
}
func textPiece(text string) piece {
return piece{main: InlineOp{Type: OpText, Text: text}}
}
func htmlPiece(html string) piece {
return piece{main: InlineOp{Type: OpRawHTML, Text: html}}
}
func (p *piece) prepend(op InlineOp) { p.before = append(p.before, op) }
func (p *piece) append(op InlineOp) { p.after = append(p.after, op) }
func (p *piece) iterate(f func(InlineOp)) {
for _, op := range p.before {
f(op)
}
f(p.main)
for i := len(p.after) - 1; i >= 0; i-- {
f(p.after[i])
}
}
// A delimiter "stack" (actually a doubly linked list), with sentinels as bottom
// and top, with the bottom being the head of the list.
//
// https://spec.commonmark.org/0.30/#delimiter-stack
type delimStack struct {
bottom, top *delim
}
func makeDelimStack() delimStack {
bottom := &delim{}
top := &delim{prev: bottom}
bottom.next = top
return delimStack{bottom, top}
}
func (s *delimStack) push(n *delim) {
n.prev = s.top.prev
n.next = s.top
s.top.prev.next = n
s.top.prev = n
}
// A node in the delimiter "stack".
type delim struct {
typ byte
bufIdx int
prev *delim
next *delim
// Only used when typ is '['
inactive bool
// Only used when typ is '_' or '*'.
n int
canOpen bool
canClose bool
}
func unlink(n *delim) {
n.next.prev = n.prev
n.prev.next = n.next
}
type linkTailParser struct {
text string
pos int
}
// Parses the link "tail", the part after the ] that closes the link text.
func parseLinkTail(text string) (n int, dest, title string) {
p := linkTailParser{text, 0}
return p.parse()
}
// https://spec.commonmark.org/0.30/#links
func (p *linkTailParser) parse() (n int, dest, title string) {
if len(p.text) < 2 || p.text[0] != '(' {
return -1, "", ""
}
p.pos = 1
p.skipWhitespaces()
if p.pos == len(p.text) {
return -1, "", ""
}
// Parse an optional link destination.
var destBuilder strings.Builder
if p.text[p.pos] == '<' {
p.pos++
closed := false
angleDest:
for p.pos < len(p.text) {
switch p.text[p.pos] {
case '>':
p.pos++
closed = true
break angleDest
case '\n', '<':
return -1, "", ""
case '\\':
destBuilder.WriteByte(p.parseBackslash())
case '&':
destBuilder.WriteString(p.parseCharRef())
default:
destBuilder.WriteByte(p.text[p.pos])
p.pos++
}
}
if !closed {
return -1, "", ""
}
} else {
parenBalance := 0
bareDest:
for p.pos < len(p.text) {
if isASCIIControl(p.text[p.pos]) || p.text[p.pos] == ' ' {
break
}
switch p.text[p.pos] {
case '(':
parenBalance++
destBuilder.WriteByte('(')
p.pos++
case ')':
if parenBalance == 0 {
break bareDest
}
parenBalance--
destBuilder.WriteByte(')')
p.pos++
case '\\':
destBuilder.WriteByte(p.parseBackslash())
case '&':
destBuilder.WriteString(p.parseCharRef())
default:
destBuilder.WriteByte(p.text[p.pos])
p.pos++
}
}
if parenBalance != 0 {
return -1, "", ""
}
}
p.skipWhitespaces()
var titleBuilder strings.Builder
if p.pos < len(p.text) && strings.ContainsRune("'\"(", rune(p.text[p.pos])) {
opener := p.text[p.pos]
closer := p.text[p.pos]
if closer == '(' {
closer = ')'
}
p.pos++
title:
for p.pos < len(p.text) {
switch p.text[p.pos] {
case closer:
p.pos++
break title
case opener:
// Titles started with "(" does not allow unescaped "(":
// https://spec.commonmark.org/0.30/#link-title
return -1, "", ""
case '\\':
titleBuilder.WriteByte(p.parseBackslash())
case '&':
titleBuilder.WriteString(p.parseCharRef())
default:
titleBuilder.WriteByte(p.text[p.pos])
p.pos++
}
}
}
p.skipWhitespaces()
if p.pos == len(p.text) || p.text[p.pos] != ')' {
return -1, "", ""
}
return p.pos + 1, destBuilder.String(), titleBuilder.String()
}
func (p *linkTailParser) skipWhitespaces() {
for p.pos < len(p.text) && isWhitespace(p.text[p.pos]) {
p.pos++
}
}
func isWhitespace(b byte) bool { return b == ' ' || b == '\t' || b == '\n' }
func (p *linkTailParser) parseBackslash() byte {
if p.pos+1 < len(p.text) && isASCIIPunct(p.text[p.pos+1]) {
b := p.text[p.pos+1]
p.pos += 2
return b
}
p.pos++
return '\\'
}
func (p *linkTailParser) parseCharRef() string {
if entity := leadingCharRef(p.text[p.pos:]); entity != "" {
p.pos += len(entity)
return UnescapeHTML(entity)
}
p.pos++
return p.text[p.pos-1 : p.pos]
}
func isASCIILetter(b byte) bool { return ('a' <= b && b <= 'z') || ('A' <= b && b <= 'Z') }
func isASCIIControl(b byte) bool { return b < 0x20 }
const asciiPuncts = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
func isASCIIPunct(b byte) bool { return strings.IndexByte(asciiPuncts, b) >= 0 }
// The CommonMark spec has its own definition of Unicode punctuation:
// https://spec.commonmark.org/0.30/#unicode-punctuation-character
//
// This definition includes all the ASCII punctuations above, some of which
// ("$+<=>^`|~" to be exact) are not considered to be punctuations by
// unicode.IsPunct.
func isUnicodePunct(r rune) bool {
return unicode.IsPunct(r) || r <= 0x7f && isASCIIPunct(byte(r))
}
const metas = "![]*_`\\&<\n"
func isMeta(b byte) bool { return strings.IndexByte(metas, b) >= 0 }