public IStringAbstraction Trim() { StringAbstraction result; switch (content) { case ContentType.Bottom: result = this; break; case ContentType.Prefix: result = new StringAbstraction(prefix.Trim(), true); break; case ContentType.SetOfCharacters: Set <SimpleCharacterAbstraction> tmp = RemoveWhiteSpaces(characters); result = new StringAbstraction(tmp); break; case ContentType.String: result = new StringAbstraction(fullstring.Trim()); break; case ContentType.Top: result = this; break; default: throw new AbstractInterpretationException("Impossible case?"); } return(result); }
public FlatAbstractDomain <bool> /*!*/ StartsWith(IStringAbstraction /*!*/ param) { FlatAbstractDomain <bool> result; FlatAbstractDomain <bool> top = new FlatAbstractDomain <bool>(false).Top; if (this.IsTop || param.IsTop) { result = top; } else { StringAbstraction what = param as StringAbstraction; //^ assert what != null; Debug.Assert(what != null); if (content == ContentType.String && what.content == ContentType.String) { result = new FlatAbstractDomain <bool>(fullstring.StartsWith(what.fullstring)); } else if (content == ContentType.Prefix && what.content == ContentType.String) { result = new FlatAbstractDomain <bool>(prefix.StartsWith(what.fullstring)); } else { result = top; } } return(result); }
public FlatAbstractDomain <int> /*!*/ CompareTo(IStringAbstraction /*!*/ param) { FlatAbstractDomain <int> result; FlatAbstractDomain <int> top = (FlatAbstractDomain <int>) new FlatAbstractDomain <int>(1).Top; if (this.IsTop || param.IsTop) { return(top); } else { StringAbstraction who = param as StringAbstraction; //^ assert who != null; Debug.Assert(who != null); if (content == ContentType.String && who.content == ContentType.String) { result = new FlatAbstractDomain <int>(fullstring.CompareTo(who.fullstring)); } else { result = top; } } return(result); }
private StringAbstraction(StringAbstraction /*!*/ a) { this.content = a.content; this.fullstring = a.fullstring; this.characters = a.characters; this.prefix = a.prefix; }
private StringAbstraction(StringAbstraction /*!*/ a) { content = a.content; fullstring = a.fullstring; characters = a.characters; prefix = a.prefix; }
static private IAbstractDomain /*!*/ HelperForStringPrefixJoin(string /*!*/ fullstring, string /*!*/ prefix) { IAbstractDomain result; if (fullstring.StartsWith(prefix)) { result = new StringAbstraction(prefix, true); } else { result = new StringAbstraction(); // i.e. top } return(result); }
static private IAbstractDomain /*!*/ HelperForPrefixJoin(string /*!*/ p1, string /*!*/ p2) { IAbstractDomain /*!*/ result; if (p1.CompareTo(p2) == 0) { // If they are the same string, nothing to do result = new StringAbstraction(p1, true); } else { result = HelperForPrefixJoin(p1, p2); } return(result); }
private IStringAbstraction EvalConstant(Expression /*!*/ exp) //^ this.decoder.IsConstant(exp); { IStringAbstraction result; string val; if (decoder.TryValueOf <string>(exp, ExpressionType.String, out val)) { result = new StringAbstraction(val); } else { result = topString; } return(result); }
static private IAbstractDomain /*!*/ HelperForStringJoin(string /*!*/ s1, string /*!*/ s2) { IAbstractDomain /*!*/ result; if (s1.CompareTo(s2) == 0) { // If they are the same string, nothing to do result = new StringAbstraction(s1); } else { // If they are different strings, try to find a common prefix, or simply abstract away all the characters int i; StringBuilder commonPrefix = new StringBuilder(); // we look for the common prefix of the two strings for (i = 0; i < Math.Min(s1.Length, s2.Length); i++) { if (s1[i] == s2[i]) { commonPrefix.Append(s1[i]); } else { break; } } if (i == 0) { // there is no common prefix Set <SimpleCharacterAbstraction> chars1 = AlphaToSetOfCharacters(s1); Set <SimpleCharacterAbstraction> chars2 = AlphaToSetOfCharacters(s2); result = new StringAbstraction(chars1.Union(chars2)); } else { result = new StringAbstraction(commonPrefix.ToString(), true); } } return(result); }
public IAbstractDomain /*!*/ Join(IAbstractDomain /*!*/ a) { IAbstractDomain tryResult; if (AbstractDomainsHelper.TryTrivialJoin(this, a, out tryResult)) { return(tryResult); } StringAbstraction right = a as StringAbstraction; //^ assert right != null; Debug.Assert(right != null, "I was expecting a " + this.GetType().ToString()); IAbstractDomain /*!*/ result; if (content == right.content) { switch (content) { #region All the cases case ContentType.Prefix: result = HelperForPrefixJoin(prefix, right.prefix); break; case ContentType.SetOfCharacters: result = HelperForSetOfCharactersJoin(characters, right.characters); break; case ContentType.String: result = HelperForStringJoin(fullstring, right.fullstring); break; case ContentType.Top: case ContentType.Bottom: default: // Impossible cases as they have already been checked before throw new AbstractInterpretationException("Impossible case?"); #endregion } } else { switch (content) { #region All the cases... case ContentType.Prefix: if (right.content == ContentType.SetOfCharacters) { result = this.Top; } else if (right.content == ContentType.String) { result = HelperForStringPrefixJoin(right.fullstring, prefix); } else { goto default; } break; case ContentType.SetOfCharacters: if (right.content == ContentType.Prefix) { result = this.Top; } else if (right.content == ContentType.String) { result = HelperForSetOfCharactersJoin(characters, AlphaToSetOfCharacters(right.fullstring)); } else { goto default; } break; case ContentType.String: if (right.content == ContentType.Prefix) { result = HelperForStringPrefixJoin(fullstring, right.prefix); } else if (right.content == ContentType.SetOfCharacters) { result = HelperForSetOfCharactersJoin(AlphaToSetOfCharacters(fullstring), right.characters); } else { goto default; } break; case ContentType.Top: case ContentType.Bottom: default: // Impossible cases as they have already been checked before throw new AbstractInterpretationException("Impossible case?"); #endregion } } return(result); }
public bool LessEqual(IAbstractDomain /*!*/ a) { bool tryResult; if (AbstractDomainsHelper.TryTrivialLessEqual(this, a, out tryResult)) { return(tryResult); } StringAbstraction right = a as StringAbstraction; //^ assert right != null; Debug.Assert(right != null, "I was expecting a " + this.GetType().ToString()); bool result; if (content == right.content) { switch (content) { #region All the cases case ContentType.Prefix: result = prefix.StartsWith(right.prefix); // it is larger if it is a substring break; case ContentType.SetOfCharacters: result = characters.IsSubset(right.characters); // it is larger if it is a subset break; case ContentType.String: result = fullstring.CompareTo(right.fullstring) == 0; // must be the same string break; case ContentType.Bottom: case ContentType.Top: default: throw new AbstractInterpretationException("Unexpected case : " + content); #endregion } } else { switch (content) { #region All the cases case ContentType.Prefix: if (right.content == ContentType.SetOfCharacters) { result = false; } else if (right.content == ContentType.String) { result = false; } else { goto default; } break; case ContentType.SetOfCharacters: if (right.content == ContentType.Prefix) { result = false; } else if (right.content == ContentType.String) { result = false; } else { goto default; } break; case ContentType.String: if (right.content == ContentType.Prefix) { result = fullstring.StartsWith(right.prefix); } else if (right.content == ContentType.SetOfCharacters) { Set <SimpleCharacterAbstraction> tmp = AlphaToSetOfCharacters(fullstring); result = tmp.IsSubset(right.characters); } else { goto default; } break; case ContentType.Bottom: case ContentType.Top: default: throw new AbstractInterpretationException("Unexpected case : " + content); #endregion } } return(result); }
public FlatAbstractDomain <bool> /*!*/ Contains(IStringAbstraction /*!*/ param) { FlatAbstractDomain <bool> result; FlatAbstractDomain <bool> top = new FlatAbstractDomain <bool>(false).Top; if (this.IsTop || param.IsTop) { result = top; } else { StringAbstraction what = param as StringAbstraction; //^ assert what != null; Debug.Assert(what != null); if (content == what.content) { #region All the cases... switch (content) { case ContentType.Prefix: result = top; break; case ContentType.SetOfCharacters: result = top; break; case ContentType.String: result = new FlatAbstractDomain <bool>(fullstring.Contains(what.fullstring)); break; default: throw new AbstractInterpretationException("Impossible case?"); } #endregion } else { switch (content) { #region All the cases case ContentType.Prefix: if (what.content == ContentType.String && prefix.Contains(what.fullstring)) { result = new FlatAbstractDomain <bool>(true); } else { result = top; } break; case ContentType.SetOfCharacters: case ContentType.String: result = top; break; default: throw new AbstractInterpretationException("Impossible case?"); #endregion } } } return(result); }
public IStringAbstraction Insert(int where, IStringAbstraction /*!*/ param) //^ requires where >= 0; { Debug.Assert(where >= 0); IStringAbstraction result; if (this.IsTop || param.IsTop) { result = CachedTop; } else { StringAbstraction what = param as StringAbstraction; //^ assert what != null; Debug.Assert(what != null); if (content == what.content) { switch (content) { #region All the cases case ContentType.Prefix: if (where < prefix.Length) { string tmp = prefix.Substring(0, where); result = new StringAbstraction(tmp + prefix, true); } else { result = CachedTop; } break; case ContentType.SetOfCharacters: result = new StringAbstraction(characters.Union(what.characters)); break; case ContentType.String: result = new StringAbstraction(fullstring.Insert(where, what.fullstring)); break; default: throw new AbstractInterpretationException("Impossible case?"); #endregion } } else { switch (content) { #region All the cases case ContentType.Prefix: if (what.content == ContentType.SetOfCharacters) { result = CachedTop; } else if (what.content == ContentType.String) { if (where < fullstring.Length) { result = new StringAbstraction(prefix.Insert(where, what.fullstring)); } else { result = this; } } else { goto default; } break; case ContentType.SetOfCharacters: if (what.content == ContentType.Prefix) { result = CachedTop; } else if (what.content == ContentType.String) { result = new StringAbstraction(characters.Union(AlphaToSetOfCharacters(what.fullstring))); } else { goto default; } break; case ContentType.String: if (what.content == ContentType.SetOfCharacters) { result = new StringAbstraction(AlphaToSetOfCharacters(fullstring).Union(what.characters)); } else if (what.content == ContentType.Prefix) { if (where < fullstring.Length) { string tmp = fullstring.Substring(0, where); result = new StringAbstraction(tmp + what.prefix, true); } else { result = this; } } else { goto default; } break; default: throw new AbstractInterpretationException("Impossible case?"); #endregion } } } return(result); }
/// <summary> /// Do the concatenation /// </summary> /// <param name="param"></param> /// <returns></returns> public IStringAbstraction ConcatWith(IStringAbstraction /*!*/ param) { IStringAbstraction result; if (this.IsTop || param.IsTop) { result = CachedTop; } else { StringAbstraction what = param as StringAbstraction; //^ assert what != null; Debug.Assert(what != null); if (content == what.content) { switch (content) { #region All the cases case ContentType.Prefix: result = this; // prefix1 \cdot prefix2 = prefix1 break; case ContentType.SetOfCharacters: result = new StringAbstraction(characters.Union(what.characters)); // { bla1 bla12 } \code {bla2 bla22} = {bla1, bla12, bla2, bla22} break; case ContentType.String: // Simple concatenation result = new StringAbstraction(fullstring + what.fullstring); break; default: throw new AbstractInterpretationException("Impossible case?"); #endregion } } else { switch (content) { #region All the cases case ContentType.Prefix: if (what.content == ContentType.SetOfCharacters) { result = this; // prefix \cdot {bla bla} = prefix } else if (what.content == ContentType.String) { // prefix \cdot string = prefix result = this; } else { goto default; } break; case ContentType.SetOfCharacters: if (what.content == ContentType.Prefix) { result = (StringAbstraction)this.Top; } else if (what.content == ContentType.String) { result = new StringAbstraction(characters.Union(AlphaToSetOfCharacters(what.fullstring))); } else { goto default; } break; case ContentType.String: if (what.content == ContentType.SetOfCharacters) { result = new StringAbstraction(AlphaToSetOfCharacters(fullstring).Union(what.characters)); } else if (what.content == ContentType.Prefix) { result = new StringAbstraction(fullstring + what.prefix, true); } else { goto default; } break; default: throw new AbstractInterpretationException("Impossible case?"); #endregion } } } return(result); }
/// <summary> /// The meet works only on the same contents /// </summary> public IAbstractDomain /*!*/ Meet(IAbstractDomain /*!*/ a) { IAbstractDomain tryResult; if (AbstractDomainsHelper.TryTrivialMeet(this, a, out tryResult)) { return(tryResult); } StringAbstraction right = a as StringAbstraction; //^ assert right != null; Debug.Assert(right != null, "I was expecting a " + this.GetType().ToString()); IAbstractDomain /*!*/ result; if (content == right.content) { switch (content) { #region All the cases ... case ContentType.Prefix: if (prefix.CompareTo(right.prefix) == 0) { result = this; } else { result = this.Bottom; } break; case ContentType.SetOfCharacters: var intersection = characters.Intersection(right.characters); if (intersection.IsEmpty) { result = this.Bottom; } else { result = new StringAbstraction(intersection); } break; case ContentType.String: if (fullstring.CompareTo(right.fullstring) == 0) { result = this; } else { result = this.Bottom; } break; case ContentType.Top: case ContentType.Bottom: default: // Impossible cases as they have already been checked before throw new AbstractInterpretationException("Impossible case?"); #endregion } } else { result = this.Bottom; } return(result); }