小编bod*_*ser的帖子

Javascript:通配符正则表达式搜索

我需要使用通配符正则表达式过滤字符串数组:

// my search key
var myKeyword = 'bar';

// my search list
var strings = ['foo', 'bar', 'foobar', 'barfoo', 'hello', 'java', 'script', 'javascript'];

// my results
var results = [];

// the regexp, I don't understand
var regex = new RegExp(\*/, myKeyword);

// the for loop
for (var i = 0; i < strings.length; i++) {
    if (regex.test(strings[i]) {
        results.push(strings[i]);
    }
}

console.log(results); // prints ['bar', 'foobar', 'barfoo']
Run Code Online (Sandbox Code Playgroud)

那么如何修复正则表达式呢?

javascript regex

1
推荐指数
1
解决办法
4248
查看次数

下划线:使用集合方法包装数组

我有一个充满对象的数组:

var myArray = [{ "user": "anton" }, { "user": "joe" }];
Run Code Online (Sandbox Code Playgroud)

我现在想把数组包装成一个Underscore Collection对象,这样我就可以使用下划线的集合方法(删除,添加):

myArray = _(myArray); // this only add object methods
myArray.add({ "user": "paul" });
Run Code Online (Sandbox Code Playgroud)

那么如何用下划线包装数组以使用下划线集合方法

javascript underscore.js

1
推荐指数
1
解决办法
3394
查看次数

JavaScript设置了四位

我得到了以下位模式:

1000 0001 (129)
Run Code Online (Sandbox Code Playgroud)

我现在想要设置后面的最后四位(1 - 10,0x1 - 0xA):

1000 0010
Run Code Online (Sandbox Code Playgroud)

要么

1000 1000
Run Code Online (Sandbox Code Playgroud)

我实际上不知道如何才能做到这一点.我可以读出前四位:

var buff = new Buffer(1);

buff[0] = 129;

var isFirstBitSet = (buff[0] & 128) == 128;
var isSecondBitSet = (buff[0] & 64) == 40;
var isThirdBitSet = (buff[0] & 32) === 32;
var isFourthBitSet = (buff[0] & 16) === 16;

var buff[0] = 0xA;

if (isFirstBitSet) {
    buff[0] = buff[0] & 128;
}
Run Code Online (Sandbox Code Playgroud)

然后在一个新的地图上映射,但我认为这是自我解释,这是废话.

javascript bit-manipulation bit bitwise-operators node.js

1
推荐指数
1
解决办法
654
查看次数

如何将迭代器(FormData)转换为对象

给定一个form元素new FormData(form)返回一个迭代器.

// [["key1", "val1"],["key2", "val2"]]
console.log([new FormData(form)...])
Run Code Online (Sandbox Code Playgroud)

我现在想将其转换为表单的对象:

{ "key1": "val1", "key2": "val2" }
Run Code Online (Sandbox Code Playgroud)

使用ES6/7功能(由babel支持).

如果可以通过功能方法实现这些想法吗?

javascript iterator ecmascript-6

1
推荐指数
2
解决办法
1059
查看次数

Symfony2:ACL允许访问带密钥的路由

我有一些CRUD路由:

/news/{page} // public
/news/new    // private
/news/{slug}/show  //public
/news/{slug}/edit  //private
/news/{slug}/delete  //private
Run Code Online (Sandbox Code Playgroud)

我需要在security.yml中的access_controll设置什么占位符?

问候

security acl roles symfony

0
推荐指数
1
解决办法
568
查看次数

安全删除对象

我有一个表,我可以在其中编辑和删除持久保存到数据库的实体类的所有对象.

当我现在转到delete-url时:保护删除的最佳做法是什么,以便用户必须确认删除?

一个简单的按钮还是有更好的解决方案?

entity symfony

0
推荐指数
1
解决办法
1087
查看次数

if语句中的函数:错误预期表达式

我想编译以下文件:

https://gist.github.com/bodokaiser/5382281

哪个不成功,因为我收到以下错误:

  CXX(target) Release/obj.target/parser/src/parser/parser.o
In file included from ../src/parser/parser.cc:2:
../src/parser/calc_head_size.cc:67:16: error: expected expression
    if (mask || isMasking(masking)) {
               ^
1 error generated.
make: *** [Release/obj.target/parser/src/parser/parser.o] Error 1
Run Code Online (Sandbox Code Playgroud)

这对我来说没有意义.是否有其他错误?

博多

c c++ compiler-errors compilation syntax-error

0
推荐指数
1
解决办法
595
查看次数

为什么pthread_cond_signal不起作用?

我目前正在学习所有POSIX线程(pthread).

我现在已经创建了一个简单的程序,它将共享值增加7直到10000以上然后它应该向下一个线程发出一个条件,它将它减少3直到1000以下.最后它应该将结果除以2并且main应该输出结果.

我的代码:

pthread_t threads[3];
pthread_cond_t cond_a, cond_b;
pthread_mutex_t mutex;

int counter;

void * worker_one();
void * worker_two();
void * worker_three();

int main(int argv, const char ** argc) {
    counter = 0;

    pthread_cond_init(&cond_a, NULL);
    pthread_cond_init(&cond_b, NULL);
    pthread_mutex_init(&mutex, NULL);

    pthread_create(&threads[0], NULL, worker_one, NULL);
    pthread_create(&threads[1], NULL, worker_two, NULL);
    pthread_create(&threads[2], NULL, worker_three, NULL);

    pthread_join(threads[0], NULL);
    pthread_join(threads[1], NULL);
    pthread_join(threads[2], NULL);

    printf("Value started at %d and ends with %d.\n", 0, counter);

    return 0;
}

void * worker_one() {
    printf("Worker one started.\n");

    pthread_mutex_lock(&mutex);

    printf("Worker one …
Run Code Online (Sandbox Code Playgroud)

c multithreading pthreads

0
推荐指数
1
解决办法
2580
查看次数

无法使用标头枚举

我想做以下事情:

parser.h

#ifndef WS_PARSER_H
#define WS_PARSER_H
#include <stdin.h>
#include <stdbool.h>

typedef enum {
    FIN = 0x80;
    RSV1 = 0x40
    /* ... */
} ws_flags;

#endif
Run Code Online (Sandbox Code Playgroud)

parser.c

#ifndef WS_PARSER
#define WS_PARSER
#include "parser.h"

ws_read_fin_flag(unsigned char * chunk) {
    return (bool) chunk[0] & WS_FLAGS.FIN;
}

#endif
Run Code Online (Sandbox Code Playgroud)

不幸的是,我得到的FIN是未声明的标识符.

我做错了什么?

更新:

全球枚举的惯例是什么?

typedef enum {} WS_FLAGS;
Run Code Online (Sandbox Code Playgroud)

c enums const header

0
推荐指数
1
解决办法
462
查看次数

指针,数组和切片的结构值

我希望有一个通用的方法,它总是返回struct值,无论它是作为指针,切片还是数组提供.

我对此的看法是:

func main() {
    p := Person{}

    if value(p).Kind() != reflect.Struct {
        fmt.Printf("Error 1")
    }
    if value(&p).Kind() != reflect.Struct {
        fmt.Printf("Error 2")
    }   
    if value([]Person{p}).Kind() != reflect.Struct {
        fmt.Printf("Error 3")
    }
    if value(&[]Person{p}).Kind() != reflect.Struct {
        fmt.Printf("Error 4")
    }
}

func value(m interface{}) reflect.Value {
    v := reflect.ValueOf(m)

    switch v.Kind() {
    case reflect.Ptr:
        v = v.Elem()

        fallthrough
    case reflect.Slice, reflect.Array:
        v = v.Elem()
    }

    return v
}
Run Code Online (Sandbox Code Playgroud)

去游乐场

正如你所看到的问题,在得到该结构出的规定slice或array.

如何扩展上面的函数以从数组或切片中获取struct值?

更新:我想要做的是[]People变成People.

reflection go

0
推荐指数
1
解决办法
637
查看次数

如何减少docker镜像安装依赖所需的时间?

我有一些节点 docker-containers,它们基本上看起来像:

# core nodejs just installs node and git on archlinux
FROM core/nodejs

# clones directory into current working dir
RUN git clone https://github.com/bodokaiser/nearby .

# installs all dependencies
RUN npm install

# lets node execute the source code
CMD ["node", "index.js"]
Run Code Online (Sandbox Code Playgroud)

当我现在重建映像以收集新更新时,它会从 npm 下载所有依赖项。这总是需要大约 5 分钟。

我现在想知道如何避免重新安装所有依赖项。

到目前为止,我的一个想法是使用VOLUME然后与主机共享代码存储库,这会使在其他主机上使用该图像变得困难。

更新: 我的另一个想法是创建一个包含 git repo 并与运行时容器共享的卷容器。但是,repo 容器必须能够以某种方式重建另一个容器?

deployment dependencies build docker

0
推荐指数
1
解决办法
1130
查看次数