Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/12/12 14:53:54 (11 years ago)
Author:
abeham
Message:

#1985:

  • Added camera icon
  • Rewrote time matching to use regular expressions
  • Changed item name of TimeLimitRun
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs

    r8977 r9040  
    2323using System.ComponentModel;
    2424using System.Linq;
     25using System.Text.RegularExpressions;
    2526using System.Windows.Forms;
    2627using HeuristicLab.Collections;
     
    4849      InitializeComponent();
    4950      snapshotButton.Text = String.Empty;
    50       snapshotButton.Image = VSImageLibrary.Breakpoint;
     51      snapshotButton.Image = VSImageLibrary.Camera;
    5152    }
    5253
     
    208209      errorProvider.SetError(timeLimitTextBox, String.Empty);
    209210
    210       var tokens = snapshotsTextBox.Text.Trim().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
    211 
    212211      var snapshotTimes = new ObservableList<TimeSpan>();
    213       foreach (var t in tokens) {
     212      var matches = Regex.Matches(snapshotsTextBox.Text, @"(\d+[ \t]*\w+)");
     213      foreach (Match m in matches) {
    214214        TimeSpan value;
    215         if (!TryGetTimeSpanFromFormat(t, out value)) {
    216           e.Cancel = true;
    217           errorProvider.SetError(snapshotsTextBox, "Error parsing " + t + ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
     215        if (!TryGetTimeSpanFromFormat(m.Value, out value)) {
     216          e.Cancel = !snapshotsTextBox.ReadOnly && snapshotsTextBox.Enabled; // don't cancel an operation that cannot be edited
     217          errorProvider.SetError(snapshotsTextBox, "Error parsing " + m.Value + ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");
    218218          return;
    219219        } else {
     
    317317    private bool TryGetTimeSpanFromFormat(string text, out TimeSpan result) {
    318318      double value;
    319       text = text.Trim();
    320       var length = text.Length;
    321       while (!double.TryParse(text.Substring(0, length), out value)) {
    322         length--;
    323         if (length <= 0) {
    324           value = double.NaN;
    325           break;
    326         }
    327       }
    328       if (double.IsNaN(value)) {
    329         result = TimeSpan.Zero;
     319      result = TimeSpan.Zero;
     320
     321      var numberMatch = Regex.Match(text, @"\d+");
     322      if (!numberMatch.Success) return false;
     323
     324      if (!double.TryParse(numberMatch.Value, out value))
    330325        return false;
    331       } else {
    332         var remaining = String.Empty;
    333         if (length < text.Length) remaining = text.Substring(length, text.Length - length);
    334         switch (remaining.Trim()) {
    335           case "d":
    336           case "day":
    337           case "days": result = TimeSpan.FromDays(value); break;
    338           case "h":
    339           case "hour":
    340           case "hours": result = TimeSpan.FromHours(value); break;
    341           case "min":
    342           case "minute":
    343           case "minutes": result = TimeSpan.FromMinutes(value); break;
    344           default: result = TimeSpan.FromSeconds(value); break;
    345         }
    346         return true;
    347       }
     326
     327      var unitMatch = Regex.Match(text, @"[a-zA-Z]+$");
     328      if (!unitMatch.Success) return false;
     329
     330      switch (unitMatch.Value) {
     331        case "d":
     332        case "day":
     333        case "days": result = TimeSpan.FromDays(value); break;
     334        case "h":
     335        case "hour":
     336        case "hours": result = TimeSpan.FromHours(value); break;
     337        case "min":
     338        case "minute":
     339        case "minutes": result = TimeSpan.FromMinutes(value); break;
     340        case "s":
     341        case "second":
     342        case "seconds": result = TimeSpan.FromSeconds(value); break;
     343        default: return false;
     344      }
     345      return true;
    348346    }
    349347  }
Note: See TracChangeset for help on using the changeset viewer.