summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/base/plain_strings.asy
blob: a466159d9c7972710b9b76c9bb71300c3fc53c32 (plain)
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
string defaultformat(int n, string trailingzero="", bool fixed=false,
                     bool signed=true)
{
  return "$%"+trailingzero+"."+string(n)+(fixed ? "f" : "g")+"$";
}

string defaultformat=defaultformat(4);
string defaultseparator="\!\times\!";

string ask(string prompt)
{
  write(stdout,prompt);
  return stdin;
}

string getstring(string name="", string default="", string prompt="",
                 bool store=true)
{
  string[] history=history(name,1);
  if(history.length > 0) default=history[0];
  if(prompt == "") prompt=name+"? [%s] ";
  prompt=replace(prompt,new string[][] {{"%s",default}});
  string s=readline(prompt,name);
  if(s == "") s=default;
  else saveline(name,s,store);
  return s;
}

int getint(string name="", int default=0, string prompt="", bool store=true)
{
  return (int) getstring(name,(string) default,prompt,store);
}

real getreal(string name="", real default=0, string prompt="", bool store=true)
{
  return (real) getstring(name,(string) default,prompt,store);
}

pair getpair(string name="", pair default=0, string prompt="", bool store=true)
{
  return (pair) getstring(name,(string) default,prompt,store);
}

triple gettriple(string name="", triple default=(0,0,0), string prompt="",
		 bool store=true)
{
  return (triple) getstring(name,(string) default,prompt,store);
}

// returns a string with all occurrences of string 'before' in string 's'
// changed to string 'after'.
string replace(string s, string before, string after) 
{
  return replace(s,new string[][] {{before,after}});
}

// Like texify but don't convert embedded TeX commands: \${}
string TeXify(string s) 
{
  static string[][] t={{"&","\&"},{"%","\%"},{"_","\_"},{"#","\#"},{"<","$<$"},
                       {">","$>$"},{"|","$|$"},{"^","$\hat{\ }$"},
                       {"~","$\tilde{\ }$"},{" ","\phantom{ }"}};
  return replace(s,t);
}

private string[][] trans1={{'\\',"\backslash "},
                           {"$","\$"},{"{","\{"},{"}","\}"}};
private string[][] trans2={{"\backslash ","$\backslash$"}};

// Convert string to TeX
string texify(string s) 
{
  return TeXify(replace(replace(s,trans1),trans2));
}

// Convert string to TeX, preserving newlines
string verbatim(string s)
{
  bool space=substr(s,0,1) == '\n';
  static string[][] t={{'\n',"\\"}};
  t.append(trans1);
  s=TeXify(replace(replace(s,t),trans2));
  return space ? "\ "+s : s;
}

// Split a string into an array of substrings delimited by delimiter
// If delimiter is an empty string, use space delimiter, discarding duplicates.
string[] split(string s, string delimiter="")
{
  bool prune=false;
  if(delimiter == "") {
    prune=true;
    delimiter=" ";
  }
  
  string[] S;
  int last=0;
  int i;
  int N=length(delimiter);
  int n=length(s);
  while((i=find(s,delimiter,last)) >= 0) {
    if(i >= last)
      S.push(substr(s,last,i-last));
    last=i+N;
    if(prune)
      while(substr(s,last,1) == " ") ++last;
  }
  if(n > last || (n == last && !prune))
    S.push(substr(s,last,n-last));
  return S;
}

int system(string s) 
{
  return system(split(s));
}

int[] operator ecast(string[] a)
{
  return sequence(new int(int i) {return (int) a[i];},a.length);
}

real[] operator ecast(string[] a)
{
  return sequence(new real(int i) {return (real) a[i];},a.length);
}

// Read contents of file as a string.
string file(string s)
{
  file f=input(s);
  string s;
  while(!eof(f)) {
    s += f+'\n';
  }
  return s;
}

string italic(string s)
{
  return s != "" ? "{\it "+s+"}" : s;
}

string baseline(string s, string template="\strut") 
{ 
  return s != "" ? "\vphantom{"+template+"}"+s : s;
}

string math(string s)
{
  return s != "" ? "$"+s+"$" : s;
}

private void notimplemented(string text) 
{
  abort(text+" is not implemented for the '"+settings.tex+"' TeX engine");
}

string jobname(string name)
{
  int pos=rfind(name,"-");
  return pos >= 0 ? "\ASYprefix\jobname"+substr(name,pos) : name;
}

string graphic(string name, string options="")
{
  if(latex()) {
    if(options != "") options="["+options+"]";
    bool pdf=pdf();
    string includegraphics="\includegraphics"+options;
    if(settings.inlinetex)
      return includegraphics+"{"+jobname(name)+"}";
    else
      return includegraphics+
        (find(name," ") < 0 ? "{"+name+"}" :
         (pdf ? "{\""+stripextension(name)+"\".pdf}" : "{\""+name+"\"}"));
  }
  if(settings.tex != "context")
    notimplemented("graphic");
  return "\externalfigure["+name+"]["+options+"]";
}

string minipage(string s, real width=100bp)
{
  if(latex())
    return "\begin{minipage}{"+(string) (width/pt)+"pt}"+s+"\end{minipage}";
  if(settings.tex != "context")
    notimplemented("minipage");
  return "\startframedtext[none][frame=off,width="+(string) (width/pt)+
    "pt]"+s+"\stopframedtext";
}

void usepackage(string s, string options="")
{
  if(!latex()) notimplemented("usepackage");
  string usepackage="\usepackage";
  if(options != "") usepackage += "["+options+"]";
  texpreamble(usepackage+"{"+s+"}");
}

void pause(string w="Hit enter to continue") 
{
  write(w);
  w=stdin;
}

string math(real x)
{
  return math((string) x);
}

string format(string format, real x, string locale="")
{
  return format(format,defaultseparator,x,locale);
}

string format(real x, string locale="")
{
  return format(defaultformat,defaultseparator,x,locale);
}

string phantom(string s)
{
  return "\phantom{"+s+"}";
}

restricted int ocgindex=0;