Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
Is there a JavaScript port of the functions in the C standard library?

I am porting some C code to JavaScript. I would like to not have to implement basic functions like strcspn myself

const strcspn = (a, b) => {
  for (let [index, c] of [...a].entries()) {
    if (b.includes(c)) {
      return index
    }
  }
  return a.length
}

Is there a project I can copy them from or that I could import?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no library in Javascript that provides the same functions of the C Standard Library (that I'm aware of, at least).

So you have to rewrite the functions yourself (not particularly difficult, as Javascript has many builtins that may help you).

Better yet: don't use C standard functions in Javascript: modify the C code directly in order to write more idiomatic Javascript code. What I mean:

// a C code that uses `strtok()` to split a string may be rewritten in JS as:
const splitted = str.split(" ");
// it's cleaner and you don't need to rewrite `strtok()` in JS

NOTE: your implementation of strcspn() is wrong. The correct implementation in Javascript would be:

function strcspn(dest, src) {
  let res = 0;
  for (let i = 0; i < dest.length; i += 1) {
    if (src.includes(dest.charAt(i))) {
      break;
    }
    res += 1;
  }
  return res;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda