0%

遇到一个蛮奇葩的问题:在 C++ 中如何不用循环和递归,打印从 1 至 N 的自然数?

想了想,用模板元编程可以解决——让编译器在编译期把该打印的东西都展开就好了。

阅读全文 »

这是一篇简单的记录。

最近在写 YTL 的过程中遇到这样一个子问题:需要定义一个函数 uint32_t func(uint32_t num),返回不小于 num 的最小的 2 的幂方。例如

1
2
3
4
5
1 == func(1);
2 == func(2);
4 == func(3);
4 == func(4);
// ……

一开始我想了个用位运算的奇技淫巧,自我感觉还不错:

阅读全文 »

去年,有人在群里提及

在 macOS 上,同样是使用 STFangSong 字体,使用 LaTeXmk 编译出来的文档在 Windows 上查看会乱码,但使用 XeLaTeX 直接编译则正常。MWE 是:

1
2
3
4
5
\documentclass{ctexbook}
\begin{document}
\fangsong
中文
\end{document}

具体的编译命令分别是:

1
2
latexmk -cd -f -xelatex -interaction=nonstopmode -synctex=1 --output-directory=output foo.tex
xelatex -interaction=nonstopmode -synctex=1 --output-directory=output foo.tex

这个问题直至近期查看了 XeLaTeX 的源码才得到了完整答案。

阅读全文 »

这是一篇简单的记录,缘起于有人在 XeLaTeX 中插入 .png 格式的图片,但提示 no boundingbox。这与我的认知不同:这一错误通常只在 LaTeX 方式编译时才会出现,而且加上 bmpsize 宏包结合 DVIPDFMx 驱动就能解决。但这次问题出现在 XeLaTeX 下,我感到很奇怪,也引起了我的兴趣。

阅读全文 »

最近在写 YTL 中的字符串相关辅助函数。实现到 split 函数时,希望能够实现类似 Python 当中的 str.split 方法的功能。

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
——https://docs.python.org/3/library/stdtypes.html#str.split

也就是说,在最基本的 split 的基础上,要添加两个功能:

  • 删除输入字符串首尾的空白;
  • 将字符串中的连续分隔符当成一个分隔符看待。

前一个功能很好实现。将空白符保存在 const char* trim_chars = " \t\n\r\v\f" 当中,然后使用 std::string::find_first_not_of 以及 std::string::find_last_not_of 即可找到有效内容的起止位置,最后再 std::string::erase 一下就好了。

后一个功能也不复杂。但要写得优雅——最好是能利用上标准库的设施——就不那么容易了。

阅读全文 »