Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!