VsExpression ParseWithSimpleFormatter(string expression) { string formatSpecifier = ""; string value = expression; int lastComma = expression.LastIndexOf(','); if (lastComma > 0) { string formatCandidate = expression.Substring(lastComma + 1).Trim(); if (RemoteValueFormatProvider.IsValidFormat(formatCandidate)) { formatSpecifier = formatCandidate; value = expression.Substring(0, lastComma); } } return(new VsExpression(value, new FormatSpecifier(formatSpecifier))); }
/// <summary> /// Tries to split the |expression| input into valid formatter suffix and a candidate /// for an expression with an expression size specifier (i.e., size specifier of the form /// [<expr>]. /// /// Note that the method does not ensure validity of |prefixWithSizeSpecifier|, it only /// splits based on a valid suffix (|baseFormatter|). /// /// Returns false if it cannot find a right bracket followed by a valid formatter suffix. /// </summary> bool TrySplitSizeSpecifierPrefix(string expression, out string prefixWithSizeSpecifier, out string baseFormatter) { prefixWithSizeSpecifier = ""; baseFormatter = ""; int rightSquareIndex = expression.LastIndexOf(']'); // If there is no right square bracket, it cannot be an expression size specifier. if (rightSquareIndex == -1) { return(false); } // If we have a right bracket, try to see if the rest of the potential formatter // is a valid suffix. string suffix = expression.Substring(rightSquareIndex + 1); if (RemoteValueFormatProvider.IsValidSizeSpecifierSuffix(suffix)) { prefixWithSizeSpecifier = expression.Substring(0, rightSquareIndex + 1); baseFormatter = suffix; return(true); } return(false); }