• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

正确地调用字符串相关的函数(第一部分)

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 11次 | 进入软件测试论坛讨论

领测软件测试网

即便在常规的程序开发过程中,程序员也常常会碰到调用字符串函数方面的困难。注意,这里的字符串函数是指有关字符指针操作相关的函数。例如,我们现有这么一个函数:reverse_string:

template< class CharType>
void reverse_string( const CharType * strDest)
{
int nLength = /* what should we call here? */;
int idxFirst = 0;
int idxLast = nLength - 1;
while ( idxFirst < idxLast)
{
std::swap( strDest[ idxFirst], strDest[ idxLast];
++ idxFirst;
-- idxLast;
)
}
在/* what should we call here? */位置,我们使用strlen来调用const char *字符串,使用wcslen来调用const wchar_t *字符串。



解决方法是建立一个t_strlen函数,如下所示:

template< class CharType>
int t_strlen( const CharType * str)
{
return strlen( str);
}

int t_strlen( const wchar_t * str)
{
return wcslen( str);
}
开始看上面代码时,可能会觉得很繁琐,因为我们必需把相关的标号(int strlen( const char *和int wcslen( const char *))与每一个函数相匹配。

这里提供了一种调用字符串函数的简单方法。这一方法很通用,它适用于几个字符串函数操作,特别是它们共用或返回相同参数的时候。例如:

适用于strlen/wcslen.
适用于 strcpy/wcscpy.
适用于 strdup/wcsdup.
假设有两个字符串函数,你将定义一个宏,如:

#define t_functionname( CharType, parameters)
string_function_<noofparameters>params< CharType, return_type>(
function_for_char, function_for_wchar_t, parameters)

// Examples

// strlen, wcslen pair
// they each have one parameter, and return an ’int’
#define t_strlen( CharType, strVal)
string_function_1param< CharType, int>( strlen, wcslen, strVal)

// strcpy, wcscpy
// they each have two parameters, and return a pointer to a string
#define t_strcpy( CharType, param1, param2)
string_function_2params< CharType, CharType*>( strcpy, wcscpy, param1, param2)

// strncpy, wcsncpy
// they each have three parameters, and return a pointer to a string
#define t_strncpy( CharType, param1, param2, param3)
string_function_3params< CharType, CharType*>( strncpy, wcsncpy, param1, param2, param3)
定义这一宏以后,在程序中调用它就会非常地简单,如: 

template< class CharType>
void reverse_string( const CharType * strDest)
{
int nLength = t_strlen( CharType, strDest);
// . . . same as before . . .
}
下面的代码的功能是正确地调用字符串相关的函数。下一部分,我们将会讨论string_function_xparams 的用途,然后为你提供一系列的宏,你可以在<string.h>头文件中调用这些宏。

/////////////////////////////////////////////////////////
// one parameter

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1&gt;
ReturnType string_function_1param_helper( CharFunction funcChar, WchartFunction funcWchart, Param1 param1, const wchar_t *)
{
return funcWchart( param1);
}

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1&gt;
ReturnType string_function_1param_helper( CharFunction funcChar, WchartFunction funcWchart, Param1 param1, ...)
{
return funcChar( param1);
}


template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1&gt;
ReturnType string_function_1param( CharFunction funcChar, WchartFunction funcWchart, Param1 param1)
{
return string_function_1param_helper&lt; CharType, ReturnType, CharFunction, WchartFunction, Param1&gt;(
funcChar, funcWchart, param1, ( const CharType *)NULL);


/////////////////////////////////////////////////////////
// two parameters

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2&gt;
ReturnType string_function_2params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2, const wchar_t *)
{
return funcWchart( param1, param2);
}

template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2&gt;
ReturnType string_function_2params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2, ...)
{
return funcChar( param1, param2);
}


template&lt; class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2&gt;
ReturnType string_function_2params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2)
{
return string_function_2params_helper&lt; CharType, ReturnType, CharFunction, WchartFunction, Param1&gt;(
funcChar, funcWchart, param1, param2, ( const CharType *)NULL);
}



/////////////////////////////////////////////////////////
// three parameters

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3>
ReturnType string_function_3params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3, const wchar_t *)
{
return funcWchart( param1, param2, param3);
}

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3>
ReturnType string_function_3params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3, ...)
{
return funcChar( param1, param2, param3);
}


template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3>
ReturnType string_function_3params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3)
{
return string_function_3params_helper< CharType, ReturnType, CharFunction, WchartFunction, Param1>(
funcChar, funcWchart, param1, param2, param3, ( const CharType *)NULL);
}

/////////////////////////////////////////////////////////
// four parameters

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4>
ReturnType string_function_4params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4, const wchar_t *)
{
return funcWchart( param1, param2, param3, param4);
}

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4>
ReturnType string_function_4params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4, ...)
{
return funcChar( param1, param2, param3, param4);
}


template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4>
ReturnType string_function_4params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4)
{
return string_function_4params_helper< CharType, ReturnType, CharFunction, WchartFunction, Param1>(
funcChar, funcWchart, param1, param2, param3, param4, ( const CharType *)NULL);
}

/////////////////////////////////////////////////////////
// five parameters

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4, class Param5>
ReturnType string_function_5params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4,
Param5 param5, const wchar_t *)
{
return funcWchart( param1, param2, param3, param4, param5);
}

template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4, class Param5>
ReturnType string_function_5params_helper(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4,
Param5 param5, ...)
{
return funcChar( param1, param2, param3, param4, param5);
}


template< class CharType, class ReturnType, class CharFunction, class WchartFunction, class Param1, class Param2, class Param3, class Param4, class Param5>
ReturnType string_function_5params(
CharFunction funcChar,
WchartFunction funcWchart,
Param1 param1,
Param2 param2,
Param3 param3,
Param4 param4,
Param5 param5)
{
return string_function_5params_helper< CharType, ReturnType, CharFunction, WchartFunction, Param1>(
funcChar, funcWchart, param1, param2, param3, param4, param5, ( const CharType *)NULL);
}


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网