* Append the given string to the buffer, with suitable quoting for passing * the string as a value in a keyword/value pair in a libpq connection string. */
| 716 | * the string as a value in a keyword/value pair in a libpq connection string. |
| 717 | */ |
| 718 | void |
| 719 | appendConnStrVal(PQExpBuffer buf, const char *str) |
| 720 | { |
| 721 | const char *s; |
| 722 | bool needquotes; |
| 723 | |
| 724 | /* |
| 725 | * If the string is one or more plain ASCII characters, no need to quote |
| 726 | * it. This is quite conservative, but better safe than sorry. |
| 727 | */ |
| 728 | needquotes = true; |
| 729 | for (s = str; *s; s++) |
| 730 | { |
| 731 | if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') || |
| 732 | (*s >= '0' && *s <= '9') || *s == '_' || *s == '.')) |
| 733 | { |
| 734 | needquotes = true; |
| 735 | break; |
| 736 | } |
| 737 | needquotes = false; |
| 738 | } |
| 739 | |
| 740 | if (needquotes) |
| 741 | { |
| 742 | appendPQExpBufferChar(buf, '\''); |
| 743 | while (*str) |
| 744 | { |
| 745 | /* ' and \ must be escaped by to \' and \\ */ |
| 746 | if (*str == '\'' || *str == '\\') |
| 747 | appendPQExpBufferChar(buf, '\\'); |
| 748 | |
| 749 | appendPQExpBufferChar(buf, *str); |
| 750 | str++; |
| 751 | } |
| 752 | appendPQExpBufferChar(buf, '\''); |
| 753 | } |
| 754 | else |
| 755 | appendPQExpBufferStr(buf, str); |
| 756 | } |
| 757 | |
| 758 | |
| 759 | /* |
no test coverage detected