Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StringExpressions.cs @ 14875

Last change on this file since 14875 was 14875, checked in by pkimmesw, 7 years ago

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File size: 17.1 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3  using System.Linq;
4
5  using Attributes;
6  using Interpreter;
7  using Stack;
8
9  [PushExpression(StackTypes.String, "STRING.FROMINTEGER", StackTypes.Integer)]
10  public class StringFromIntegerExpression : StatelessExpression {
11    public override bool Eval(IInternalPushInterpreter interpreter) {
12      if (interpreter.IntegerStack.IsEmpty)
13        return false;
14
15      var value = interpreter.IntegerStack.Pop();
16      interpreter.StringStack.Push(value.ToString());
17      return true;
18    }
19  }
20
21  [PushExpression(StackTypes.String, "STRING.FROMFLOAT", StackTypes.Float)]
22  public class StringFromFloatExpression : StatelessExpression {
23    public override bool Eval(IInternalPushInterpreter interpreter) {
24      if (interpreter.FloatStack.IsEmpty)
25        return false;
26
27      var value = interpreter.FloatStack.Pop();
28      interpreter.StringStack.Push(value.ToString());
29      return true;
30    }
31  }
32
33  [PushExpression(StackTypes.String, "STRING.FROMBOOLEAN", StackTypes.Boolean)]
34  public class StringFromBooleanExpression : StatelessExpression {
35    public override bool Eval(IInternalPushInterpreter interpreter) {
36      if (interpreter.BooleanStack.IsEmpty)
37        return false;
38
39      var value = interpreter.BooleanStack.Pop();
40      interpreter.StringStack.Push(value.ToString());
41      return true;
42    }
43  }
44
45  [PushExpression(StackTypes.String, "STRING.FROMCHAR", StackTypes.Char)]
46  public class StringFromCharExpression : StatelessExpression {
47    public override bool Eval(IInternalPushInterpreter interpreter) {
48      if (interpreter.CharStack.IsEmpty)
49        return false;
50
51      var value = interpreter.CharStack.Pop();
52      interpreter.StringStack.Push(value.ToString());
53      return true;
54    }
55  }
56
57  [PushExpression(StackTypes.String, "STRING.CONCAT")]
58  public class StringConcatExpression : StatelessExpression {
59    public override bool Eval(IInternalPushInterpreter interpreter) {
60      if (interpreter.StringStack.Count < 2 ||
61        interpreter.StringStack.Top.Length + interpreter.StringStack[1].Length >= interpreter.Configuration.MaxStringLength)
62        return false;
63
64      var str = interpreter.StringStack.Pop();
65      interpreter.StringStack.SetTop(str + interpreter.StringStack.Top);
66      return true;
67    }
68  }
69
70  /// <summary>
71  /// Conj char onto string
72  /// </summary>
73  [PushExpression(StackTypes.String, "STRING.CONJCHAR", StackTypes.Char)]
74  public class StringConjCharExpression : StatelessExpression {
75    public override bool Eval(IInternalPushInterpreter interpreter) {
76      if (interpreter.StringStack.IsEmpty ||
77          interpreter.CharStack.IsEmpty ||
78          interpreter.StringStack.Top.Length + 1 >= interpreter.Configuration.MaxStringLength)
79        return false;
80
81      var c = interpreter.CharStack.Pop();
82      interpreter.StringStack.SetTop(interpreter.StringStack.Top + c);
83      return true;
84    }
85  }
86
87  [PushExpression(StackTypes.String, "STRING.TAKE", StackTypes.Integer)]
88  public class StringTakeExpression : StatelessExpression {
89    public override bool Eval(IInternalPushInterpreter interpreter) {
90      if (interpreter.StringStack.IsEmpty ||
91          interpreter.IntegerStack.IsEmpty)
92        return false;
93
94      var value = interpreter.IntegerStack.Pop();
95
96      if (value < 0) {
97        return true;
98      }
99
100      var str = interpreter.StringStack.Top;
101
102      if (str.Length > 0) {
103        value = Math.Min(str.Length - 1, value);
104        interpreter.StringStack.SetTop(str.Substring(0, (int)value));
105      }
106
107      return true;
108    }
109  }
110
111  [PushExpression(StackTypes.String, "STRING.SUBSTRING", StackTypes.Integer)]
112  public class StringSubstringExpression : StatelessExpression {
113    public override bool Eval(IInternalPushInterpreter interpreter) {
114      if (interpreter.StringStack.IsEmpty ||
115          interpreter.IntegerStack.Count < 2)
116        return false;
117
118      var str = interpreter.StringStack.Top;
119      var firstValue = interpreter.IntegerStack[1];
120      var secondValue = interpreter.IntegerStack.Top;
121      interpreter.IntegerStack.Remove(2);
122
123      var first = Math.Min(str.Length - 1, Math.Max(firstValue, 0));
124      var second = Math.Min(str.Length - 1, Math.Max(secondValue, first));
125      var length = second - first;
126
127      if (length > 0)
128        interpreter.StringStack.SetTop(str.Substring((int)first, (int)length));
129
130      return true;
131    }
132  }
133
134  [PushExpression(StackTypes.String, "STRING.FIRST")]
135  public class StringFirstExpression : StatelessExpression {
136    public override bool Eval(IInternalPushInterpreter interpreter) {
137      if (interpreter.StringStack.IsEmpty ||
138          interpreter.StringStack.Top.Length == 0)
139        return false;
140
141      interpreter.StringStack.SetTop(interpreter.StringStack.Top[0].ToString());
142      return true;
143    }
144  }
145
146
147  [PushExpression(StackTypes.String, "STRING.LAST")]
148  public class StringLastExpression : StatelessExpression {
149    public override bool Eval(IInternalPushInterpreter interpreter) {
150      if (interpreter.StringStack.IsEmpty ||
151          interpreter.StringStack.Top.Length == 0)
152        return false;
153
154      var str = interpreter.StringStack.Top;
155      var c = str[str.Length - 1].ToString();
156      interpreter.StringStack.SetTop(c);
157      return true;
158    }
159  }
160
161  [PushExpression(StackTypes.String, "STRING.NTH", StackTypes.Integer)]
162  public class StringNthExpression : StatelessExpression {
163    public override bool Eval(IInternalPushInterpreter interpreter) {
164      if (interpreter.StringStack.IsEmpty ||
165          interpreter.IntegerStack.IsEmpty ||
166          interpreter.StringStack.Top.Length == 0)
167        return false;
168
169      var str = interpreter.StringStack.Top;
170      var index = str.Length == 1 ? 0 : (int)Math.Abs(interpreter.IntegerStack.Pop() % (str.Length - 1));
171      var c = str[index].ToString();
172      interpreter.StringStack.SetTop(c);
173      return true;
174    }
175  }
176
177  [PushExpression(StackTypes.String, "STRING.REST")]
178  public class StringRestExpression : StatelessExpression {
179    public override bool Eval(IInternalPushInterpreter interpreter) {
180      if (interpreter.StringStack.IsEmpty ||
181          interpreter.StringStack.Top.Length == 0)
182        return false;
183
184      var str = interpreter.StringStack.Top;
185      interpreter.StringStack.SetTop(str.Length == 1 ? string.Empty : str.Substring(1, str.Length - 1));
186      return true;
187    }
188  }
189
190  [PushExpression(StackTypes.String, "STRING.BUTLAST")]
191  public class StringButLastExpression : StatelessExpression {
192    public override bool Eval(IInternalPushInterpreter interpreter) {
193      if (interpreter.StringStack.IsEmpty ||
194          interpreter.StringStack.Top.Length == 0)
195        return false;
196
197      var str = interpreter.StringStack.Top;
198      interpreter.StringStack.SetTop(str.Length == 1 ? string.Empty : str.Substring(0, str.Length - 1));
199      return true;
200    }
201  }
202
203  [PushExpression(StackTypes.String, "STRING.LENGTH", StackTypes.Integer)]
204  public class StringLengthExpression : StatelessExpression {
205    public override bool Eval(IInternalPushInterpreter interpreter) {
206      if (interpreter.StringStack.IsEmpty)
207        return false;
208
209      var str = interpreter.StringStack.Pop();
210      interpreter.IntegerStack.Push(str.Length);
211      return true;
212    }
213  }
214
215  [PushExpression(StackTypes.String, "STRING.REVERSE")]
216  public class StringReverseExpression : StatelessExpression {
217    public override bool Eval(IInternalPushInterpreter interpreter) {
218      if (interpreter.StringStack.IsEmpty)
219        return false;
220
221      interpreter.StringStack.SetTop(interpreter.StringStack.Top.Reverse().ToString());
222      return true;
223    }
224  }
225
226  [PushExpression(StackTypes.String, "STRING.PARSETOCHARS")]
227  public class StringParseToCharsExpression : StatelessExpression {
228    public override bool Eval(IInternalPushInterpreter interpreter) {
229      if (interpreter.StringStack.IsEmpty)
230        return false;
231
232      if (interpreter.StringStack.Top.Length == 0) {
233        interpreter.StringStack.Pop();
234        return true;
235      }
236
237      var str = interpreter.StringStack.Top;
238      interpreter.StringStack.SetTop(str[0].ToString());
239
240      if (str.Length > 1) {
241        var chars = new string[str.Length - 1];
242        for (var i = 0; i < str.Length - 1; i++) {
243          chars[i] = str[i + 1].ToString();
244        }
245
246        interpreter.StringStack.Push(chars);
247      }
248
249      return true;
250    }
251  }
252
253  [PushExpression(StackTypes.String, "STRING.SPLIT")]
254  public class StringSplitExpression : StatelessExpression {
255    public override bool Eval(IInternalPushInterpreter interpreter) {
256      if (interpreter.StringStack.IsEmpty)
257        return false;
258
259      var words = interpreter.StringStack.Top.Trim().Split();
260
261      if (words.Length == 0)
262        return false;
263
264      interpreter.StringStack.SetTop(words[0]);
265
266      if (words.Length > 1)
267        interpreter.StringStack.Push(words, 1);
268
269      return true;
270    }
271  }
272
273  /// <summary>
274  /// True if top string is empty
275  /// </summary>
276
277  [PushExpression(StackTypes.String, "STRING.EMPTYSTRING", StackTypes.Boolean)]
278  public class StringEmptyStringExpression : StatelessExpression {
279    public override bool Eval(IInternalPushInterpreter interpreter) {
280      if (interpreter.StringStack.IsEmpty)
281        return false;
282
283      var str = interpreter.StringStack.Pop();
284      interpreter.BooleanStack.Push(str.Length == 0);
285      return true;
286    }
287  }
288
289  /// <summary>
290  /// True if top string is a substring of second string; false otherwise
291  /// </summary>
292  [PushExpression(StackTypes.String, "STRING.CONTAINS", StackTypes.Boolean)]
293  public class StringContainsExpression : StatelessExpression {
294    public override bool Eval(IInternalPushInterpreter interpreter) {
295      if (interpreter.StringStack.Count < 2)
296        return false;
297
298      var first = interpreter.StringStack[1];
299      var second = interpreter.StringStack.Top;
300      interpreter.StringStack.Remove(2);
301
302      interpreter.BooleanStack.Push(first.IndexOf(second, StringComparison.Ordinal) >= 0);
303      return true;
304    }
305  }
306
307  /// <summary>
308  /// True if the top char is in the top string
309  /// </summary>
310  [PushExpression(StackTypes.String, "STRING.CONTAINSCHAR", StackTypes.Boolean | StackTypes.Char)]
311  public class StringContainsCharExpression : StatelessExpression {
312    public override bool Eval(IInternalPushInterpreter interpreter) {
313      if (interpreter.StringStack.IsEmpty ||
314          interpreter.CharStack.IsEmpty)
315        return false;
316
317      var str = interpreter.StringStack.Pop();
318      var c = interpreter.CharStack.Pop();
319
320      interpreter.BooleanStack.Push(str.IndexOf(c) >= 0);
321      return true;
322    }
323  }
324
325  /// <summary>
326  /// Puts on the integer stack the index of the top char in the top string
327  /// </summary>
328  [PushExpression(StackTypes.String, "STRING.INDEXOFCHAR", StackTypes.Integer | StackTypes.Char)]
329  public class StringIndexOfCharExpression : StatelessExpression {
330    public override bool Eval(IInternalPushInterpreter interpreter) {
331      if (interpreter.StringStack.IsEmpty ||
332          interpreter.CharStack.IsEmpty)
333        return false;
334
335      var str = interpreter.StringStack.Pop();
336      var c = interpreter.CharStack.Pop();
337
338      interpreter.IntegerStack.Push(str.IndexOf(c));
339      return true;
340    }
341  }
342
343  /// <summary>
344  /// The number of times the top char is in the top string
345  /// </summary>
346  [PushExpression(StackTypes.String, "STRING.OCCURENCESOFCHAR", StackTypes.Integer | StackTypes.Char)]
347  public class StringOccurrencesOfCharExpression : StatelessExpression {
348    public override bool Eval(IInternalPushInterpreter interpreter) {
349      if (interpreter.StringStack.IsEmpty ||
350          interpreter.CharStack.IsEmpty)
351        return false;
352
353      var str = interpreter.StringStack.Pop();
354      var c = interpreter.CharStack.Pop();
355
356      var count = 0;
357      for (var i = 0; i < str.Length; i++)
358        if (str[i] == c) count++;
359
360      interpreter.IntegerStack.Push(count);
361      return true;
362    }
363  }
364
365  /// <summary>
366  /// In third string on stack, replaces second string with first string
367  /// </summary>
368  [PushExpression(StackTypes.String, "STRING.REPLACE")]
369  public class StringReplaceExpression : StatelessExpression {
370    public override bool Eval(IInternalPushInterpreter interpreter) {
371      if (interpreter.StringStack.Count < 3)
372        return false;
373
374      var first = interpreter.StringStack[1];
375      var second = interpreter.StringStack.Top;
376      interpreter.StringStack.Remove(2);
377
378      if (first.Length == 0 || second.Length == 0)
379        interpreter.StringStack.SetTop(first);
380      else {
381        var result = first.Replace(second, interpreter.StringStack.Top);
382        interpreter.StringStack.SetTop(result);
383      }
384
385      return true;
386    }
387  }
388
389  /// <summary>
390  /// In third string on stack, replaces first occurence of second string with first string
391  /// </summary>
392  [PushExpression(StackTypes.String, "STRING.REPLACEFIRST")]
393  public class StringReplaceLastExpression : StatelessExpression {
394    public override bool Eval(IInternalPushInterpreter interpreter) {
395      if (interpreter.StringStack.Count < 3)
396        return false;
397
398      var first = interpreter.StringStack[0];
399      var second = interpreter.StringStack[1];
400      var third = interpreter.StringStack[2];
401      interpreter.StringStack.Remove(2);
402
403      var result = ReplaceFirst(third, second, first);
404      interpreter.StringStack.SetTop(result);
405      return true;
406    }
407
408    private static string ReplaceFirst(string text, string search, string replace) {
409      int pos = text.IndexOf(search);
410      if (pos < 0) {
411        return text;
412      }
413      return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
414    }
415  }
416
417  /// <summary>
418  /// In top string on stack, replaces all occurences of second char with first char
419  /// </summary>
420  [PushExpression(StackTypes.String, "STRING.REPLACECHAR", StackTypes.Char)]
421  public class StringReplaceCharExpression : StatelessExpression {
422    public override bool Eval(IInternalPushInterpreter interpreter) {
423      if (interpreter.StringStack.IsEmpty ||
424          interpreter.CharStack.Count < 2)
425        return false;
426
427      var first = interpreter.CharStack[1];
428      var second = interpreter.CharStack.Top;
429      interpreter.CharStack.Remove(2);
430
431      var result = interpreter.StringStack.Top.Replace(first, second);
432      interpreter.StringStack.SetTop(result);
433      return true;
434    }
435  }
436
437  /// <summary>
438  /// In top string on stack, replaces first occurence of second char with first char
439  /// </summary>
440  [PushExpression(StackTypes.String, "STRING.REPLACEFIRSTCHAR", StackTypes.Char)]
441  public class StringReplaceLastCharExpression : StatelessExpression {
442    public override bool Eval(IInternalPushInterpreter interpreter) {
443      if (interpreter.StringStack.IsEmpty ||
444          interpreter.CharStack.Count < 2)
445        return false;
446
447      var str = interpreter.StringStack.Top;
448      var first = interpreter.CharStack[1];
449      var second = interpreter.CharStack.Top;
450      interpreter.CharStack.Remove(2);
451
452      var pos = str.IndexOf(first);
453
454      if (pos < 0)
455        return true;
456
457      var result = str.Substring(0, pos) +
458                   second +
459                   str.Substring(Math.Min(pos + 2, str.Length - 1));
460
461      interpreter.StringStack.SetTop(result);
462      return true;
463    }
464  }
465
466  /// <summary>
467  /// In top string on stack, remove all occurences of char
468  /// </summary>
469  [PushExpression(StackTypes.String, "STRING.REMOVECHAR", StackTypes.Char)]
470  public class StringRemoveCharExpression : StatelessExpression {
471    public override bool Eval(IInternalPushInterpreter interpreter) {
472      if (interpreter.StringStack.IsEmpty ||
473          interpreter.CharStack.IsEmpty)
474        return false;
475
476      var c = interpreter.CharStack.Pop();
477      var result = interpreter.StringStack.Top.Trim(c);
478      interpreter.StringStack.SetTop(result);
479      return true;
480    }
481  }
482
483  /// <summary>
484  /// Returns a function that sets char at index in string
485  /// </summary>
486  [PushExpression(StackTypes.String, "STRING.SETCHAR", StackTypes.Char | StackTypes.Integer)]
487  public class StringSetCharExpression : StatelessExpression {
488    public override bool Eval(IInternalPushInterpreter interpreter) {
489      if (interpreter.StringStack.IsEmpty ||
490          interpreter.CharStack.IsEmpty ||
491          interpreter.IntegerStack.IsEmpty)
492        return false;
493
494      var str = interpreter.StringStack.Top;
495      var i = (int)interpreter.IntegerStack.Pop();
496      var c = interpreter.CharStack.Pop();
497
498      if (str.Length == 0) {
499        interpreter.StringStack.Pop();
500        return true;
501      }
502
503      var pos = str.Length == 1 ? 0 : Math.Abs(i) % (str.Length - 1);
504      var result = str.Substring(0, pos) + c + str.Substring(Math.Min(pos + 2, str.Length - 1));
505
506      interpreter.StringStack.SetTop(result);
507      return true;
508    }
509  }
510}
Note: See TracBrowser for help on using the repository browser.