septillion:
I've lost a bit what you actually want to do with the function... I see replace but at runtime you can't replace parts of a flash string... But whatever you want to do, you might not need the complete string all the time.
For example, if you want to compare a string, you just need one char at a time. If it's the same, move on and check again. No need to load the whole string in memory 
I was trying to avoid two things simultaneously Septillion.
-
Avoid having to duplicate the main algorithm in ALL of my replace(...) functions using different combinations of the _P and non _P versions of the string and mem functions.
-
Avoid having to make copies of flash strings in normal global memory and run the risk of memory overrun and corruption.
Sometimes it's better to drop the build in function if it causes headaches.
And for the second past, yeah, you try to do that while writing the function. But what is the actual function need to do? 
boylesg:
I was trying to avoid two things simultaneously Septillion.
-
Avoid having to duplicate the main algorithm in ALL of my replace(...) functions using different combinations of the _P and non _P versions of the string and mem functions.
-
Avoid having to make copies of flash strings in normal global memory and run the risk of memory overrun and corruption.
For example you have parallel functions, one of which operates on program and one on normal memory:
size_t strlcat_P ( char * dst,
const char * src,
size_t siz
)
and
size_t strlcat ( char * dst,
const char * src,
size_t siz
)
and you want to use the same algorithm for both.
You can't use the same call for both because the pointer types can't be distinguished. But you can use the same algorithm if you make those wrapper functions for a base function that is passed the pointer type as an argument. You further create a pair of indexing functions, one for normal and one for flash strings. The algorithm then selects one or the other indexing methods depending on the pointer type that it has been passed.
aarg:
For example you have parallel functions, one of which operates on program and one on normal memory:
size_t strlcat_P ( char * dst,
const char * src,
size_t siz
)
and
size_t strlcat ( char * dst,
const char * src,
size_t siz
)
and you want to use the same algorithm for both.
You can't use the same call for both because the pointer types can't be distinguished. But you can use the same algorithm if you make those wrapper functions for a base function that is passed the pointer type as an argument. You further create a pair of indexing functions, one for normal and one for flash strings. The algorithm then selects one or the other indexing methods depending on the pointer type that it has been passed.
Understand what you are saying Aarg, but I specifically chose the word 'algorithm' not the word 'code'. Because an identical algorithm can be implemented with either versions of the string and mem functions.
I presume this thread is related to your previous thread?
boylesg:
Is there any way to do this?
No. It is generally impossible to determine if a pointer is meant for flash or RAM by examining the type or value. The only reason the F() macro works is that it casts the pointer to a helper type (__FlashStringHelper*), which let's the compiler pick the correct overload of the print() or println() functions.
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
size_t print(unsigned char, int = DEC);
size_t print(int, int = DEC);
size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC);
size_t print(unsigned long, int = DEC);
size_t print(double, int = 2);
size_t print(const Printable&);
size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
size_t println(unsigned char, int = DEC);
size_t println(int, int = DEC);
size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC);
size_t println(unsigned long, int = DEC);
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
If you manually declare the variable with the PROGMEM attribute, the type will be no different than any other const variable. Without the cast to a helper type, there is no way for you to overload a function to handle both flash and RAM values.