1 /*
2  * Some utility functions on VTermRect structures
3  */
4 
5 #define STRFrect "(%d,%d-%d,%d)"
6 #define ARGSrect(r) (r).start_row, (r).start_col, (r).end_row, (r).end_col
7 
8 /* Expand dst to contain src as well */
rect_expand(VTermRect * dst,VTermRect * src)9 static void rect_expand(VTermRect *dst, VTermRect *src)
10 {
11   if(dst->start_row > src->start_row) dst->start_row = src->start_row;
12   if(dst->start_col > src->start_col) dst->start_col = src->start_col;
13   if(dst->end_row   < src->end_row)   dst->end_row   = src->end_row;
14   if(dst->end_col   < src->end_col)   dst->end_col   = src->end_col;
15 }
16 
17 /* Clip the dst to ensure it does not step outside of bounds */
rect_clip(VTermRect * dst,VTermRect * bounds)18 static void rect_clip(VTermRect *dst, VTermRect *bounds)
19 {
20   if(dst->start_row < bounds->start_row) dst->start_row = bounds->start_row;
21   if(dst->start_col < bounds->start_col) dst->start_col = bounds->start_col;
22   if(dst->end_row   > bounds->end_row)   dst->end_row   = bounds->end_row;
23   if(dst->end_col   > bounds->end_col)   dst->end_col   = bounds->end_col;
24   /* Ensure it doesn't end up negatively-sized */
25   if(dst->end_row < dst->start_row) dst->end_row = dst->start_row;
26   if(dst->end_col < dst->start_col) dst->end_col = dst->start_col;
27 }
28 
29 /* True if the two rectangles are equal */
rect_equal(VTermRect * a,VTermRect * b)30 static int rect_equal(VTermRect *a, VTermRect *b)
31 {
32   return (a->start_row == b->start_row) &&
33          (a->start_col == b->start_col) &&
34          (a->end_row   == b->end_row)   &&
35          (a->end_col   == b->end_col);
36 }
37 
38 /* True if small is contained entirely within big */
rect_contains(VTermRect * big,VTermRect * small)39 static int rect_contains(VTermRect *big, VTermRect *small)
40 {
41   if(small->start_row < big->start_row) return 0;
42   if(small->start_col < big->start_col) return 0;
43   if(small->end_row   > big->end_row)   return 0;
44   if(small->end_col   > big->end_col)   return 0;
45   return 1;
46 }
47 
48 /* True if the rectangles overlap at all */
rect_intersects(VTermRect * a,VTermRect * b)49 static int rect_intersects(VTermRect *a, VTermRect *b)
50 {
51   if(a->start_row > b->end_row || b->start_row > a->end_row)
52     return 0;
53   if(a->start_col > b->end_col || b->start_col > a->end_col)
54     return 0;
55   return 1;
56 }
57