小编Dir*_*irk的帖子

如何将C字符串转换为Rust字符串并通过FFI返回?

我正在尝试获取C库返回的C字符串,并通过FFI将其转换为Rust字符串.

mylib.c

const char* hello(){
    return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)

main.rs

#![feature(link_args)]

extern crate libc;
use libc::c_char;

#[link_args = "-L . -I . -lmylib"]
extern {
    fn hello() -> *c_char;
}

fn main() {
    //how do I get a str representation of hello() here?
}
Run Code Online (Sandbox Code Playgroud)

c ffi rust

47
推荐指数
2
解决办法
2万
查看次数

适合数学符号的良好网络字体

我正在 HTML5 中开发一个计算器应用程序,需要打印一些数学符号,例如平方根和 pi,但问题是这些符号在大多数网络字体中都没有定义,因此它们将以默认字体出现,看起来只是与其他输入相比太可怕了。有没有对数学符号有良好支持的网络字体(最好是 Google Fonts)?

webfonts google-webfonts

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

去简单的API网关代理

我一直在网上搜索如何做到这一点,但我一直无法找到它.我正在尝试使用Go和Martini为我的系统构建一个简单的API网关,该系统有一些运行REST接口的微服务.例如,我users运行了我的服务192.168.2.8:8000,我想通过它访问它/users

所以我的API网关看起来像这样:

package main

import (
    "github.com/codegangsta/martini"
    "net/http"
)

func main(){
    app := martini.Classic()
    app.Get("/users/:resource", func(req *http.Request, res http.ResponseWriter){
        //proxy to http://192.168.2.8:8000/:resource
    })
    app.Run()
}
Run Code Online (Sandbox Code Playgroud)


编辑


我有一些工作,但我所看到的是[vhost v2] release 2.2.5:

package main

import(
    "net/url"
    "net/http"
    "net/http/httputil"
    "github.com/codegangsta/martini"
    "fmt"
)

func main() {
    remote, err := url.Parse("http://127.0.0.1:3000")
    if err != nil {
        panic(err)
    }

    proxy := httputil.NewSingleHostReverseProxy(remote)
    app := martini.Classic()
    app.Get("/users/**", handler(proxy))
    app.RunOnAddr(":4000")
}

func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request, martini.Params) {
    return func(w http.ResponseWriter, …
Run Code Online (Sandbox Code Playgroud)

proxy go martini

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

Lua:这会导致段错误

我正在开发一个使用Lua编写脚本的程序,有时它会崩溃.使用GDB我认为我发现了问题,但我不知道它是否解决了它,因为段错误只会偶尔发生.所以,旧代码是这样的:

void Call(std::string func){
    lua_getglobal(L, func.c_str()); //This is the line GDB mentioned in a backtrace
    if( lua_isfunction(L,lua_gettop(L)) ) {
        int err = lua_pcall(L, 0, 0,0 );
        if(err != 0){
            std::cout << "Lua error: " << luaL_checkstring(L, -1) << std::endl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,这个函数每秒会被调用几次,但它需要调用的函数并不总是被定义,所以我认为堆栈会溢出.我添加了以下行:

lua_pop(L,lua_gettop(L));
Run Code Online (Sandbox Code Playgroud)

并且不再发生段错误了.这可能是问题吗?

c++ lua

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

Qt5 OpenGL GLSL 版本错误

我开始在 Qt 和着色器中使用 OpenGL(我有 OpenGL 经验,但还没有使用着色器)

我正在关注本教程:http: //releases.qt-project.org/learning/developerguides/qtopengltutorial/OpenGLTutorial.pdf(官方 Qt5 OpenGL 教程)。

问题是,当我尝试运行我的程序时,出现黑屏和以下错误消息:

QGLShader::compile(Vertex): ERROR: 0:1: '' :  version '130' is not supported

QGLShader::compile(Fragment): ERROR: 0:1: '' :  version '130' is not supported
Run Code Online (Sandbox Code Playgroud)

我的程序基于 QGLWidget

通过在互联网上的一些浏览,我发现我需要使用 OpenGL 3.2 上下文,但 Qt 喜欢使用 OpenGL 2.x

我的电脑:

  • MacBook pro Retina '15,2012 年末
  • 英特尔高清 4000
  • 英伟达 GeForce 650M

那么,我怎样才能使这项工作?

编辑:

我的版本是 3.2(通过 QGLFormat 设置),没有指定格式它使用 2.0

fragmentShader.frag:

#version 130

uniform vec4 color;

out vec4 fragColor;

void main(void)
{
    fragColor = color;
}
Run Code Online (Sandbox Code Playgroud)

vertexShader.vert:

#version 130 …
Run Code Online (Sandbox Code Playgroud)

c++ opengl macos qt glsl

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

Java RegEx 查找,引号之间除外

我需要一个 Java RegEx 来拆分或在字符串中查找某些内容,但排除双引号之间的内容。我现在要做的是:

String withoutQuotes = str.replaceAll("\\\".*?\\\"", "placeholder");
withoutQuotes = withoutQuotes.replaceAll(" ","");
Run Code Online (Sandbox Code Playgroud)

但这不适用于 indexOf,而且我还需要能够拆分,例如:

String str = "hello;world;how;\"are;you?\""
String[] strArray = str.split(/*some regex*/);
// strArray now contains: ["hello", "world", "how", "\"are you?\"]
Run Code Online (Sandbox Code Playgroud)
  • 报价总是平衡的
  • 引号可以转义 \"

任何帮助表示赞赏

java regex quotes split

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

AngularJS routeprovider注入错误

我正在学习AngularJS,我现在正在尝试$routeProvider,但我无法让它工作.

index.html:

<!DOCTYPE html>

<html ng-app="App">
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular-route.min.js"></script>
        <script type="text/javascript" src="scripts/controllers.js"></script>
    </head>
    <body>
        <div ng-view></div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

view.html:

<p>Hello World!</p>
Run Code Online (Sandbox Code Playgroud)

controllers.js:

var app = angular.module('App', ['ngRoute']);

app.config(
    function($routeProvider){
        $routeProvider.when("/something",{
            templateUrl: "view.html",
            controller: "MyController"
        })
        .otherwhise({
            redirectTo: "/"
        });
    }
);

function MyController($scope){

}
Run Code Online (Sandbox Code Playgroud)

每当我运行它时,我会收到一条错误消息

Uncaught Error: [$injector:modulerr]
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

html javascript angularjs

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

这个语法有什么名称

在语言中,人们可以做以下事情:

let num = 5.add(3)
Run Code Online (Sandbox Code Playgroud)

这将是相同的

let num = add(5,3)
Run Code Online (Sandbox Code Playgroud)

所以,基本上你把点之前的表达式作为函数的第一个参数.我敢肯定其他语言都有这个功能,但没有一个直接想到.

我想知道的是这种语法的名称

nimrod nim-lang

4
推荐指数
2
解决办法
211
查看次数

Apple Blocks vs C++ 11 Lambdas

我一直在玩C++ 11和Apple块,我试图创建一种interator函数.代码:

#include <functional>
#include <stdio.h>

void range(int low, int high, int& ref, std::function<void(void)> block){
    int a = low;
    while(a < high){
        ref = a;
        block();
        a++;
    }
}

void range(int low, int high, int& ref, void (^block)(void)){
    int a = low;
    while(a < high){
        ref = a;
        block();
        a++;
    }
}

int main(){
    int a = 0;
    range(0, 5, a, [&](){
        printf("%i\n", a);
    });

    int b = 0;
    range(0, 5, b, ^(){
        printf("%i\n", b);
    });
}
Run Code Online (Sandbox Code Playgroud)

第一个,使用C++ 11 Lambdas按预期工作,并给出以下输出 …

c++ lambda block objective-c c++11

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

C++ + SDL + OpenGL 3.3在Mac OS X上不起作用?

我正在开始开发OpenGL 3(我习惯于1,所以这是一个很大的改变),我正在使用SDL作为我的窗口/图像/声音/事件框架.我有以下代码(取自opengl.org并稍加修改):

#include <stdio.h>
#include <stdlib.h>
/* If using gl3.h */
/* Ensure we are using opengl's core profile only */
#define GL3_PROTOTYPES 1
#include <OpenGL/gl3.h>

#include <SDL2/SDL.h>
#define PROGRAM_NAME "Tutorial1"

/* A simple function that prints a message, the error code returned by SDL,
 * and quits the application */
void sdldie(const char *msg)
{
    printf("%s: %s\n", msg, SDL_GetError());
    SDL_Quit();
    exit(1);
}


void checkSDLError(int line = -1)
{
#ifndef NDEBUG
    const char *error = SDL_GetError();
    if (*error != '\0') …
Run Code Online (Sandbox Code Playgroud)

c++ opengl macos sdl osx-mountain-lion

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

javascript正则表达式替换括号之间的空格

如果在括号之间,我如何使用 JS 正则表达式用单词 SPACE 替换所有出现的空格?所以,我想要的是:

myString = "a scentence (another scentence between brackets)"
myReplacedString = myString.replace(/*some regex*/)
//myReplacedString is now "a scentence (anotherSPACEscentenceSPACEbetweenSPACEbrackets)"
Run Code Online (Sandbox Code Playgroud)

编辑:我试过的是这个(我对正则表达式很陌生)

myReplacedString = myString.replace(/\(\s\)/, "SPACE");
Run Code Online (Sandbox Code Playgroud)

javascript regex

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