Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does nothing but tokenization. But this also makes it dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

###C++ Version

C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does nothing but tokenization. But this also makes it dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

###C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does nothing but tokenization. But this also makes it dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}
Fix typos
Source Link
janos
  • 113k
  • 15
  • 154
  • 396

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does not nothing but tokenization. But this also makes ait dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

###C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does not nothing but tokenization. But this also makes a dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

###C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does nothing but tokenization. But this also makes it dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

###C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

I used strtok to tokenize a string. But many developers said me to avoid strtok, why?

Because strtok() modifies the input is the main reason.
But it has other issues that make it not a good option in modern code.

How should I optimize this code?

Well let us make it correct first before we go for optimizations. But strtok() will probably be the fastest. That's because it basically does not nothing but tokenization. But this also makes a dangerous to use.

Is it really wrong that I used strtok?

Yes if you are using C++. The better technique is to code for type safety and re-usability. The difference in speed will be minimal. So small that if this program is running for years that even the cumulative time saves by strtok() will not amount to the time taken to fix a single bug by a human.

If so then with what should I replace it?

You should probably use the C++ stream operators.

###C++ Version

 // Slightly different to the original.
 // If the value part has an illegal character for a float it
 // will probably mess up.
 DWORD FN_attack_speed_from_file(char const* fileName)
 {
 std::ifstream file(fileName);
 std::string field;
 float value;
 while(file >> field >> value)
 {
 if (field == "DirectInputTime") {
 return value * 1000.0;
 }
 }
 return 1000;
}
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /