一些代码片段

1.不要用return语句返回指向“栈内存”指针。

char* getmem(int num)
{
char *p = (char*)malloc(sizeof(char)*num);
return p;
}

int main(int argc, char* argv[])
{
char *str = NULL;
str = getmem(12);
strcpy(str, "Hello fuck!");
cout<<str<<endl;
free(str);
return 0;
}

这种方式是可以的,因为返回的不是“栈内存”指针。所谓的“栈内存”指针是指固定大小的数组、
变量等。

2.一个不错的字符串 hash 函数

unsigned long hash(const char *name,size_t len)
{
unsigned long h=(unsigned long)len;
size_t step = (len>>5)+1;
for (size_t i=len; i>=step; i-=step)
h = h ^ ((h<<5)+(h>>2)+(unsigned long)name[i-1]);
return h;
}

一个方便的 hash 函数应该散列的比较开,计算速度跟字符串长度关系不大,又不能只计算字符
串的开头或末尾。这里的算法是从Lua 中看来的。

3.UTF8 到 UTF16 的转换(单个字符)

int UTF8toUTF16(int c)
{
signed char *t=(signed char*)&c;
int ret=*t &(0x0f | (~(*t>>1) &0x1f) | ~(*t>>7));
int i;
assert ((*t & 0xc0) != 0x80);
for (i=1;i<3;i++) {
if ((t[i] & 0xc0)!=0x80) {
break;
}
ret=ret<<6 | (t[i] & 0x3f);
}
return ret;
}

这只是一个字符的转换,如果转换字符串,可以再做一点优化。

4.宽字符和窄字符转换的问题

BOOL UnicodeToAnsi(LPWSTR pszwUniString, LPSTR pszAnsiBuff, DWORD dwAnsiBuffSize)
{
int iRet = 0;
iRet = WideCharToMultiByte(CP_ACP, 0, pszwUniString, -1, pszAnsiBuff,
dwAnsiBuffSize, NULL, NULL);
return (0 != iRet);
}

BOOL AnsiToUnicode(LPSTR pszAnsiString, LPWSTR pszwUniBuff, DWORD dwUniBuffSize)
{
int iRet = 0;
iRet = MultiByteToWideChar(CP_ACP, 0, pszAnsiString, -1, pszwUniBuff,
dwUniBuffSize);
return (0 != iRet);
}


// 应用示例

LPSTR lpCmdLine;
LPWSTR lpwCmdLine;
AnsiToUnicode(lpCmdLine, lpwCmdLine, sizeof(lpwCmdLine));
lpCmdLine = NULL;
UnicodeToAnsi(lpwCmdLine, lpCmdLine, sizeof(lpwCmdLine));
static_cast<char*>(lpCmdLine);
// Function that safely converts a 'WCHAR' String to 'LPSTR':
char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn)
{
LPSTR pszOut = NULL;
if (lpwszStrIn != NULL)
{
int nInputStrLen = wcslen (lpwszStrIn);
// Double NULL Termination
int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
pszOut = new char [nOutputStrLen];
if (pszOut)
{
memset (pszOut, 0x00, nOutputStrLen);
WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut,
nOutputStrLen, 0, 0);
}
}
return pszOut;
}
Verilog代码片段是在编程过程中重复使用的一段代码。它可以是一小段功能代码,也可以是一整个模块的代码。这些代码片段可以被保存并在需要的时候被调用,从而提高编程效率。在使用Verilog的编辑器中,如VS Code和Notepad,你可以利用插件来支持Verilog代码片段的编写和使用。 在VS Code中,有一个名为"Verilog Snippets"的插件可以用来支持Verilog代码片段的使用。通过这个插件,你可以自定义Verilog代码片段,并在编写代码时快速调用它们。你可以根据自己的喜好和需求编辑这些代码片段,使其更符合你的编码风格和习惯。 Notepad是另一个支持Verilog代码片段的编辑器。它自带Verilog语法识别功能,并且有一些插件可以帮助你实现代码片段的功能。你可以利用这些插件在Notepad界面中编写Verilog代码片段,并设置触发字来调用它们。这样,在编写Verilog代码时,只需输入触发字然后按下Tab键,就可以自动插入相应的代码片段,非常方便。 通过使用Verilog代码片段,你可以事先准备好常用的Verilog代码段落,并在需要的时候快速调用它们。这样可以大大提高编程的效率,节省时间和精力。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [verilog_snippets:VS Code中将使用Verilog HDL的一些代码片段](https://download.csdn.net/download/weixin_42179184/16910033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Notepad++编辑器——Verilog代码片段和语法检查](https://blog.csdn.net/baidu_34971492/article/details/106659799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值