现在我正在使用此函数来确定数字是否为整数:
function isInteger(input){
return (input%1==0);
}
有更有效的方法吗?我知道这里有一个内置的javascript函数,但它在某些IE版本中不起作用。
答案 0 :(得分:3)
根据jsPerf,~~
(repeated bitwise NOT)和<< 0
(向左移0位)在我的浏览器中更快:
function isInteger(input) {
return (~~input === input);
}
function isInteger2(input) {
return (input << 0 === input);
}
警告:JavaScript按位运算符将其操作数强制转换为signed 32-bit integers,因此此方法仅对
Math.pow(2, 31) -1
(2147483647)之前的数字安全。如果您不能保证,请使用其他方法。
在其他方法中,n % 1 === 0
和Math.floor(value) === value
的速度慢了40-50%,Number.isInteger
落后了。
然而,它们都“非常快”。除非您必须检查批次数字,否则这些方法都不会成为您应用程序的瓶颈。
答案 1 :(得分:1)
bool parse(const char* line, char* abs_path, char* query)
{
// TODO
// Get the lenght of line
int l = strlen(line);
// Store the place in the string
int a = 0;
// Stores number of total spaces
int spaces = 0;
// Iterate in line
for (int i = 0; i < l; i++)
{// Look for spaces
if (isspace(line[i]) == 0)
{
spaces++;
}
}
// If there are more than the 2 required spaces
if (spaces > 2)
{
error(400);
printf("400 Bad Request\n");
return false;
}
// If request - target doen't begin with "/"
// Look for the fist space
for (int i = 0; i < l; i++)
{// Look for space
if (isspace(line[i]) == 0)
{
a = i + 1;
break;
}
}
if (line[a] != '/')
{
error(501);
printf("501 not implemented\n");
return false;
}
{
error(405);
printf("405 Method Not Allowed\n");
return false;
}
// If request-target contains "
// Will store position of second space
int b = 0;
// Look for the second space
for (int i = a; i < l; i++)
{
if (isspace(line[i]) == 0)
{
b = i;
break;
}
}
// Find if there is a " in request-target
for (int i = a; i < b; i++)
{
if ( line[i] == '"')
{
error(400);
printf("400 Bad Request\n");
return false;
}
}
// If HTTP version isn't HTTP/1.1
char http[7] = "";
for (int i = b + 1; i < (b + 1) + 7/*Chars
from the second space till the end of HTTP/1.1*/;i++)
{
http[i] = line[i];
}
if (strcmp(http, "HTTP/1.1") != 0)
{
error(505);
printf("505 HTTP Version Not Supported\n");
return false;
}
// Store absolute-path at the address in abs_path and the query string at query
// Separate the query
// String to store the query
char query2[8190 + 1] = "";
// Store the position in line where the query string starts
int c = 0;
// Counter for query array
int q = 0;
for (int i = b - 1/*before the space after the query*/; i > a/*after the first space*/;i--)
{
if (line[i] == '=')
{
c = i - 1;
break;
}
}
// Pass the string to query2
for (int i = c; i < (b - 1); i++)
{
query2[q] = line[i];
q++;
}
// If there is no query, make query2 empty
if (query2[0] != 'q')
{
strcpy(query2, "");
}
// Allocate query2 and query at the heap
query = malloc(sizeof(query2));
// Copy query2 to query
strcpy(query, query2);
// Separate absolute path
// Declare char array to store absolute path
char abs1[8190 + 1] = "";
// Make space at the heap
abs_path = malloc(sizeof(abs1));
for (int i = a /*where the / after the first space starts*/; i < (c - 1);
i++)
{
abs1[i] = line[i];
}
// Copy abs to abs_path
strcpy(abs_path, abs1);
return true;
}
答案 2 :(得分:-2)
TextInfo.ToTitleCase()