Skip to content

Commit 679c276

Browse files
committed
提取辅助函数
1 parent a5941eb commit 679c276

3 files changed

Lines changed: 48 additions & 80 deletions

File tree

src/System/Language/code_wrap.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef TM_CODE_WRAP_HPP
2+
#define TM_CODE_WRAP_HPP
3+
4+
#include "basic.hpp"
5+
#include "string.hpp"
6+
// Protect TeXmacs internal escape sequences like "<#4E2D>" (CJK, etc.)
7+
// from being split during automatic line wrapping in code/prog environments.
8+
//
9+
// NOTE:
10+
// - We only protect "<#...>" to avoid affecting normal code like "<tag>".
11+
// - This is a last-resort safety net: even if the line breaker proposes an
12+
// invalid split position, we snap it to a valid boundary here.
13+
static inline int
14+
tm_atom_end_for_code_wrap (string s, int i) {
15+
int n= N (s);
16+
if (i < 0 || i >= n) return i;
17+
if (s[i] != '<') return i + 1;
18+
19+
if (i + 1 >= n || s[i + 1] != '#') return i + 1;
20+
21+
int j= i + 2;
22+
while (j < n && s[j] != '>')
23+
j++;
24+
if (j < n && s[j] == '>') return j + 1;
25+
26+
return i + 1;
27+
}
28+
29+
static inline int
30+
tm_snap_after_boundary_for_code_wrap (string s, int after) {
31+
int n= N (s);
32+
if (after <= 0) return 0;
33+
if (after >= n) return n;
34+
35+
int i = 0;
36+
int last= 0;
37+
while (i < n) {
38+
int j= tm_atom_end_for_code_wrap (s, i);
39+
if (j > after) break;
40+
last= j;
41+
i = j;
42+
}
43+
return last;
44+
}
45+
46+
#endif // TM_CODE_WRAP_HPP

src/System/Language/prog_language.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212

1313
#include "analyze.hpp"
14+
#include "code_wrap.hpp"
1415
#include "convert.hpp"
1516
#include "converter.hpp"
1617
#include "cork.hpp"
@@ -21,46 +22,6 @@
2122
#include "tm_url.hpp"
2223
#include "tree_helper.hpp"
2324

24-
// Protect TeXmacs internal escape sequences like "<#4E2D>" (CJK, etc.)
25-
// from being split during automatic line wrapping in prog/code environments.
26-
static inline int
27-
tm_atom_end_for_code_wrap (string s, int i) {
28-
int n= N (s);
29-
if (i < 0 || i >= n) return i;
30-
if (s[i] != '<') return i + 1;
31-
32-
// Only treat "<#...>" as an indivisible atom to avoid affecting normal code
33-
// like "<tag>"
34-
if (i + 1 >= n || s[i + 1] != '#') return i + 1;
35-
36-
int j= i + 2;
37-
while (j < n && s[j] != '>')
38-
j++;
39-
if (j < n && s[j] == '>') return j + 1;
40-
41-
// malformed sequence: degrade gracefully
42-
return i + 1;
43-
}
44-
45-
// Snap "after" to the greatest atom boundary <= after, so we never split inside
46-
// "<#...>".
47-
static inline int
48-
tm_snap_after_boundary_for_code_wrap (string s, int after) {
49-
int n= N (s);
50-
if (after <= 0) return 0;
51-
if (after >= n) return n;
52-
53-
int i = 0;
54-
int last= 0;
55-
while (i < n) {
56-
int j= tm_atom_end_for_code_wrap (s, i);
57-
if (j > after) break;
58-
last= j;
59-
i = j;
60-
}
61-
return last;
62-
}
63-
6425
prog_language_rep::prog_language_rep (string name)
6526
: abstract_language_rep (name) {
6627
if (DEBUG_PARSER)

src/System/Language/verb_language.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
******************************************************************************/
1111

1212
#include "analyze.hpp"
13+
#include "code_wrap.hpp"
1314
#include "impl_language.hpp"
1415
#include "observers.hpp"
1516
#include "packrat.hpp"
@@ -24,46 +25,6 @@ is_sep_char (char c) {
2425
return c == '-' || c == '/' || c == '\\' || c == ',' || c == '?';
2526
}
2627

27-
// Protect TeXmacs internal escape sequences like "<#4E2D>" (CJK, etc.)
28-
// from being split during automatic line wrapping in code/prog environments.
29-
static inline int
30-
tm_atom_end_for_code_wrap (string s, int i) {
31-
int n= N (s);
32-
if (i < 0 || i >= n) return i;
33-
if (s[i] != '<') return i + 1;
34-
35-
// Only treat "<#...>" as an indivisible atom to avoid affecting normal code
36-
// like "<tag>"
37-
if (i + 1 >= n || s[i + 1] != '#') return i + 1;
38-
39-
int j= i + 2;
40-
while (j < n && s[j] != '>')
41-
j++;
42-
if (j < n && s[j] == '>') return j + 1;
43-
44-
// malformed sequence: degrade gracefully
45-
return i + 1;
46-
}
47-
48-
// Snap "after" to the greatest atom boundary <= after, so we never split inside
49-
// "<#...>".
50-
static inline int
51-
tm_snap_after_boundary_for_code_wrap (string s, int after) {
52-
int n= N (s);
53-
if (after <= 0) return 0;
54-
if (after >= n) return n;
55-
56-
int i = 0;
57-
int last= 0;
58-
while (i < n) {
59-
int j= tm_atom_end_for_code_wrap (s, i);
60-
if (j > after) break;
61-
last= j;
62-
i = j;
63-
}
64-
return last;
65-
}
66-
6728
text_property
6829
verb_language_rep::advance (tree t, int& pos) {
6930
string s= t->label;

0 commit comments

Comments
 (0)