注意:这不是“ 是否可以在Makefile中创建多行字符串变量”和其他类似问题的副本。这个问题要求不依赖纯Bash或仅GNU Make的功能的POSIX兼容解决方案。关于堆栈溢出的另一个问题没有此要求。
sed在macOS High Sierra上使用BSD获取以下shell脚本:
printf 'foo\nbaz\n' | sed '/foo/a\
bar
'
Run Code Online (Sandbox Code Playgroud)
输出为:
foo
bar
baz
Run Code Online (Sandbox Code Playgroud)
我怎样才能把同样的东西放进去Makefile?这是我尝试过的方法,但似乎不起作用:
all:
printf 'foo\nbaz\n' | sed '/foo/a\\
bar\
'
Run Code Online (Sandbox Code Playgroud)
这会导致错误:
$ make
printf 'foo\nbaz\n' | sed '/foo/a\\
/bin/sh: -c: line 0: unexpected EOF while looking for matching `''
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
我如何才能sed在Makefile中正确编写上述调用,以便在make执行all目标时将尾部斜杠和换行符正确地输入到shell中?
注意:我希望Makefile兼容POSIX,并且仅使用http://pubs.opengroup.org/onlinepubs/9699919799/utilities/make.html中的功能。
我的问题:
有什么区别overflow-wrap: break-word和word-break: break-word?
非重复:
以下是一些现有的问题,乍一看似乎是重复的,但实际上并非如此。
word-break: break-all但我的问题是关于word-break: break-word)overflow-wrap和word-break属性,但我在这里的问题特别是关于break-word这个属性的值。此外,该问题被神秘地标记为上一个问题的重复,即使它不相关。)代码:
我写这似乎表明,无论此代码段overflow-wrap: break-word和word-break: break-word行为方式相同。但我不确定我是否已经考虑了所有情况。也许在某些情况下他们的行为有所不同?
.ow {
overflow-wrap: break-word;
background: lightgreen;
}
.wb {
word-break: break-word;
background: lightblue;
}
div {
width: 5em;
display: inline-block;
}Run Code Online (Sandbox Code Playgroud)
<div class="ow">
Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
</div>
<div class="wb">
Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
</div>
<div class="ow">
Most words are short and don't need to …Run Code Online (Sandbox Code Playgroud)代码:
from unittest.mock import MagicMock, call
mm = MagicMock()
mm().foo()['bar']
print(mm.mock_calls)
print()
mm.assert_has_calls([call(), call().foo(), call().foo().__getitem__('bar')])
Run Code Online (Sandbox Code Playgroud)
输出:
[call(), call().foo(), call().foo().__getitem__('bar')]
Traceback (most recent call last):
File "foo.py", line 9, in <module>
mm.assert_has_calls([call(), call().foo(), call().foo().__getitem__('bar')])
TypeError: tuple indices must be integers or slices, not str
Run Code Online (Sandbox Code Playgroud)
如何修复这个断言?
注意:这不是现有std::ios::sync_with_stdio(false)问题的重复。我已经经历了所有这些,但我无法cout像printf. 示例代码和证据如下所示。
我有三个源代码文件:
// ex1.cpp
#include <cstdio>
#include <chrono>
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; i++) {
printf("%d\n", i);
}
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1);
fprintf(stderr, "%lld\n", duration.count());
}
Run Code Online (Sandbox Code Playgroud)
// ex2.cpp
#include <iostream>
#include <chrono>
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < 10000000; i++) {
std::cout << i << '\n';
}
auto t2 …Run Code Online (Sandbox Code Playgroud) 我正在尝试用 ASM 组装一小段代码。此代码将 CX 设置为零,将 AX 设置为 1。我的代码:
mov cx, 0000h
mov ax, ffffh
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
Run Code Online (Sandbox Code Playgroud)$ nasm foo.asm foo.asm:2: error: symbol `ffffh' not defined
我可以通过编写mov ax, 0ffffh来解决此错误。但为什么不明白ffffh语法呢?NASM 文档中的哪个位置指定了允许使用哪些十六进制语法,不允许使用哪些十六进制语法?
我读了https://nasm.us/doc/nasmdoc3.html#section-3.4.1但找不到任何不允许的内容ffffh语法的内容。我缺少什么?
我也阅读了作为对此问题的评论提供的其他一些类似问题。但它们似乎都没有指出一些权威文档或规范来确认数字必须以数字开头。如果有人可以指出 NASM 文档中的确切摘录或某些规范来证实这一点,那就可以回答这个问题。
注意:建议的重复项处理的是unsigned intand signed int,而不是unsigned char和signed char。建议的重复问题涉及 C11。这个问题只与C89有关。这个问题可以重新讨论吗?
我的代码:
#include <stdio.h>
int main()
{
signed char c;
unsigned char d;
c = (signed char) -2;
d = (unsigned char) c;
printf("%d %d\n", c, d);
d = (unsigned char) 254;
c = (signed char) d;
printf("%d %d\n", c, d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ clang -Wall -Wextra -pedantic -std=c89 foo.c && ./a.out
-2 254
-2 254
Run Code Online (Sandbox Code Playgroud)
对于上面显示的两种转换,输出是否保证-2 254在符合标准的 C89 编译器中?或者输出取决于实施?
我正在编写这样的程序,将一些字节写入文件.
#include <fstream>
int main()
{
char buffer[4] = {0, 0, 255, 255};
std::ofstream f("foo.txt", std::ios_base::binary);
f.write(buffer, sizeof buffer);
f.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这很好,并在我的系统上给我预期的结果.
$ g++ -std=c++11 -pedantic -Wall -Wextra signedness.cc
$ ./a.out
$ cat foo.txt
$ od -t x1 foo.txt
0000000 00 00 ff ff
0000004
Run Code Online (Sandbox Code Playgroud)
等效的C代码是:
#include <stdio.h>
int main()
{
char buffer[4] = {0, 0, 255, 255};
FILE *f = fopen("bar.txt", "wb");
fwrite(buffer, sizeof *buffer, sizeof buffer, f);
fclose(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序也可以正常工作,并在我的系统上提供预期的输出.
我想知道上述将字节写入文件的方法是否合适.
C++ n3242.pdf的第4.7节(积分转换)在第3点中提到: …
我正在编写这个小演示代码来启动 HTTP 服务器,测试它是否成功运行,然后退出。
import http.server
import urllib.request
import threading
# Start HTTP server
httpd = http.server.HTTPServer(('', 8080), http.server.SimpleHTTPRequestHandler)
thread = threading.Thread(target=httpd.serve_forever)
thread.start()
# Test HTTP server and shutdown
print(urllib.request.urlopen('http://127.0.0.1:8080/').read())
httpd.shutdown()
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,但由于运气,它可能工作正常。您看,我httpd.serve_forever在一个单独的线程中调用,这样我的脚本就不会在此调用处阻塞,并且可以继续调用urllib.request.urlopen以测试 HTTP 服务器。
但我必须确保urllib.request.urlopen仅在httpd.serve_forever成功调用并且服务器现在正在处理请求后才进行调用。
http.server.HTTPServer或中是否有任何标志或任何方法socketserver.TCPServer可以帮助我确保urllib.request.urlopen只有在它准备好服务请求后才开始测试它?
这是一个简单的 WSGI 应用程序,它应该在标头中打印 Content-Length 和请求正文。
def application(environ, start_response):
start_response('200 OK', [('Content-Type','text/plain')])
content_length = int(environ['CONTENT_LENGTH'])
print('---- Begin ----')
print('CONTENT_LENGTH:', content_length)
print('wsgi.input:', environ['wsgi.input'].read())
print('---- End ----')
return [b'Foo\n']
if __name__ == '__main__':
from wsgiref import simple_server
server = simple_server.make_server('0.0.0.0', 8080, application)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)
当我运行此应用程序时,它在以下调用中被阻止:environ['wsgi.input'].read().
我使用 Python 3 解释器运行该应用程序,并使用 curl 向它提交 HTTP post 请求。
lone@debian:~$ curl --data "a=1&b=2" http://localhost:8080/
Run Code Online (Sandbox Code Playgroud)
curl 命令被阻塞等待输出。python 解释器在environ['wsgi.input'].read()调用时被阻塞。
lone@debian:~$ python3 foo.py
---- Begin ----
CONTENT_LENGTH: 7
Run Code Online (Sandbox Code Playgroud)
正如您在上面的输出中看到的,该application()函数在 print 后被阻塞CONTENT_LENGTH。
我知道如何解决这个问题:通过将 …
我在这个JSFiddle中演示了我的问题:http://jsfiddle.net/6jJaF/
如果右键单击图像周围的红色单元格,然后单击"检查元素",您将看到即使IMG元素的高度为200px,其周围的TR和TD元素的高度也各为205px.为什么它的高度大于图像?如何摆脱这个额外的高度?
如果TD元素的内容是文本而不是IMG,则不会出现此问题,如本示例中的第二个表中所示.
这是HTML代码.
<table>
<tr>
<td><img src="http://i.imgur.com/dj7aqdo.jpg"/></td>
</tr>
</table>
<table>
<tr>
<td>Foo</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
这是CSS代码.
td {
padding: 0;
background: red;
height: 200px;
}
img {
height: 200px;
}
Run Code Online (Sandbox Code Playgroud)