与代码契约String.IsNullOrWhitespace的.NET 3.5实施 [英] .Net 3.5 Implementation of String.IsNullOrWhitespace with Code Contracts

查看:357
本文介绍了与代码契约String.IsNullOrWhitespace的.NET 3.5实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的.Net 3.5(C#)项目中使用合同。我发现,我曾写过类似如果(string.IsNullOrEmpty(S)|| string.IsNullOrEmpty(s.Trim()))抛出新的ArgumentException(必需,S); 在方法的开始。



我可以让他们有和使用传统的合同。我可以将其更改为 Contract.Requires(S = NULL&放大器;!&安培; string.IsNullOrEmpty(s.Trim())); 。但是,除了不是很肯定的的空格的条件的一部分,这些都是跛脚(意见)。



相反,正确性或性能,我试图封装条件在合同中使用的方法。问题是,我仍然无法弄清楚如何以声明描述的空格的一部分。静态检查取决于我怎么写它发现几个问题。



我想听听关于数据的内容检查的恰当性意见,如这一个空格以及一个正确的定义。

 公共静态类STR 
{
[纯]
公共静态布尔IsNullOrWhitespace(字符串s)
{
Contract.Ensures(S =空|| Contract.Result&下;!布尔>());
Contract.Ensures((S =空&放大器;!&放大器;!Contract.ForAll&所述;烧焦&将(S,C => char.IsWhiteSpace(c)项))|| Contract.Result&所述;布尔>()) ;

如果(S == NULL)返回true;

为(VAR I = 0; I< s.Length;我++)
{
如果
{(char.IsWhiteSpace(S,I)!)
Contract.Assume(Contract.ForAll<!烧焦>(S,C => char.IsWhiteSpace(C)));
返回FALSE;
}
}


返回真;
}
}

这可能是草率的,但我最初的实验方法这里。在A,B的断言和c是失败或没有得到证明。

 静态无效测试()
{
字符串= NULL;
字符串B =;
串c =;
串D =XYZ;
串E =XYZ;

如果
{
Contract.Assert(A!= NULL)(str.IsNullOrWhitespace(一)!);
A = a.Trim();
}

如果
{
Contract.Assert(str.IsNullOrWhitespace(B)!)(B = NULL&放大器;!和b =!) ;
B = b.Trim(); (!str.IsNullOrWhitespace(C))
}

如果
{
Contract.Assert(C =空&安培;!和C =!) ;
C = c.Trim();
}

如果
{
D = d.Trim()(str.IsNullOrWhitespace(D)!); (!str.IsNullOrWhitespace(E))
}

如果
{
E = e.Trim();
}
}


解决方案

下面是从.NET 4内置合同:

  Contract.Ensures(
(Contract.Result<布尔>( )及及((STR == NULL)||(str.Length == 0)))
?((布尔)真)
:((Contract.Result<布尔>() ||(STR == NULL))
((布尔)FALSE)
:((布尔)(str.Length大于0)))

,空, Contract.Result<布尔>()及及(STR == NULL || str.Length == 0)|| \r\\\
Contract.Result<!布尔>()及和放大器;海峡! = NULL&放大器;&放大器; str.Length大于0);



尽管它的复杂性,你可以看到,有没有空白的提及。我试过几次,但我不能得到的东西的工作,将满足静态检查。



有似乎是一对夫妇,从写它阻止我的问题,所以我提交的代码合同论坛的一些问题。



我的合同是:

  Contract.Ensures(Contract.Result<布尔>()== 
(S == NULL || Contract.Exists(S,char.IsWhiteSpace)))


I'm attempting to use Contracts in my .Net 3.5 (C#) project. I found where I had written something like if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(s.Trim())) throw new ArgumentException("Required", "s"); at the beginning of methods.

I could keep them there and use legacy contracts. I could change it to Contract.Requires(s!= null && string.IsNullOrEmpty(s.Trim()));. But besides not being very sure about the correctness or performance of the Whitespace part of the condition, those are lame (opinion).

Instead, I tried to encapsulate the condition in a method for use in Contracts. Problem is that I still can't figure out how to describe the Whitespace part declaratively. The static checker finds several issues depending on how I write it.

I'd like to hear comments about the appropriateness of data content checks such as this one for whitespace AS WELL AS a correct definition.

public static class str
{
    [Pure]
    public static bool IsNullOrWhitespace(string s)
    {
      Contract.Ensures(s != null || Contract.Result<bool>());
      Contract.Ensures((s != null && !Contract.ForAll<char>(s, c => char.IsWhiteSpace(c))) || Contract.Result<bool>());

      if (s == null) return true;

      for (var i = 0; i < s.Length; i++)
      {
        if (!char.IsWhiteSpace(s, i))
        {
          Contract.Assume(!Contract.ForAll<char>(s, c => char.IsWhiteSpace(c)));
          return false;
        }
      }


      return true;
    }
}

This might be sloppy, but my initial experimentation method is here. The Asserts on a, b, and c are failing or are unproven.

static void Test()
{
  string a = null;
  string b = "";
  string c = "     ";
  string d = "xyz";
  string e = "  xyz  ";

  if (!str.IsNullOrWhitespace(a))
  {
    Contract.Assert(a != null);
    a = a.Trim();
  }

  if (!str.IsNullOrWhitespace(b))
  {
    Contract.Assert(b != null && b != "");
    b = b.Trim();
  }

  if (!str.IsNullOrWhitespace(c))
  {
    Contract.Assert(c != null && c != "     ");
    c = c.Trim();
  }

  if (!str.IsNullOrWhitespace(d))
  {
    d = d.Trim();
  }

  if (!str.IsNullOrWhitespace(e))
  {
    e = e.Trim();
  }
}

解决方案

Here's the built-in contract from .NET 4:

Contract.Ensures(
    (Contract.Result<bool>() && ((str == null) || (str.Length == 0)))
    ? ((bool) true)
    : ((Contract.Result<bool>() || (str == null))
      ? ((bool) false)
      : ((bool) (str.Length > 0)))

, null, "Contract.Result<bool>() && (str == null || str.Length == 0) ||\r\n!Contract.Result<bool>() && str != null && str.Length > 0");

Despite its complexity, you can see that there is no mention of whitespace. I've tried several times, but I can't get something working that will satisfy the static checker.

There appear to be a couple of issues stopping me from writing it, so I've filed some questions on the Code Contracts forum.

My contract was:

Contract.Ensures(Contract.Result<bool>() ==
                 (s == null || Contract.Exists(s, char.IsWhiteSpace)))

这篇关于与代码契约String.IsNullOrWhitespace的.NET 3.5实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆